• 0

Another c++ problem (noob)


Question

Here is my code. the problem is that it only lets you input for the first cout statement then lists the rest of the cout statements without allowing input then ends. idk what going wrong and why the compiler is doing this.

snipcput.th.jpg

#include <iostream>

using namespace std;

//Write a program that computes the cost of a long-distance call. The cost of

//the call is determined according to the following rate schedule:

//a. Any call started between 8:00 A.M. and 6:00 P.M., Monday through

//Friday, is billed at a rate of $0.40 per minute.

//b. Any call starting before 8:00 A.M. or after 6:00 P.M., Monday through

//Friday, is charged at a rate of $0.25 per minute.

//c. Any call started on a Saturday or Sunday is charged at a rate of $0.15

//per minute.

//The input will consist of the day of the week, the time the call started,

//and the length of the call in minutes. The output will be the cost of the

//call. The time is to be input in 24-hour notation, so the time 1:30 P.M. is

//input as

//13:30

//The day of the week will be read as one of the following pairs of character

//values, which are stored in two variables of type char:

//Mo Tu We Th Fr Sa Su

//Be sure to allow the user to use either uppercase or lowercase letters or a

//combination of the two. The number of minutes will be input as a value

//of type int. (You can assume that the user rounds the input to a whole

//number of minutes.) Your program should include a loop that lets the

//user repeat this calculation until the user says she or he is done.

char choice;

float rate_1=0.45, rate_2=0.25, rate_3=0.15;

int main (int argc, char* argv[])

{

do{

cout<<"This program will compute the total cost of a long distance call \n";

cout<<"To determine the cost please provide for us the following information...\n";

cout<<"Mo Tu We Th Fr Sa Su\n";

char day;

int mtime, time, min;

cout<<endl;

cout<<"Please enter what day you will be making your call then press enter:";

cin>>day;

cout<<endl;

cout<<"Please enter the time the call started in 'Military time'\n";

cout<<"Enter the hours then press enter:";

cin>>mtime;

cout<<endl;

cout<<"Enter the minutes:\n";

cin>>min;

cout<<endl;

cout<<"Please enter the length of the time in minutes <numbers only> of the call then press enter: ";

cin>>time;

cout<<endl;

if ((day=='MO')||(day=='Mo')||(day=='mo')||(day=='TU')||(day=='Tu')||(day=='tu')||(day=='we')||(day=='We')||(day=='WE')||(day=='TH')||(day=='Th')||(day=='th')||(day=='fr')||(day=='Fr')||(day=='FR'))

{

if (((mtime>=8)&&(mtime<=18))&&((min>=0)&&(min<60)))

{

float you_owe;

you_owe=time*rate_1; //$0.40 per minute

cout<<"You will be charged"<<"$"<<you_owe<<" "<<"for the call. \n";

}

else

{

float youre_owe;

youre_owe=time*rate_2; //$0.25 per minute

cout<<"You will be charged"<<"$"<<youre_owe<<" "<<"for the call. \n";

}

}

if ((day=='Su')||(day=='SU')||(day=='su')||(day=='sa')||(day=='SA')||(day=='Sa'))

{

float u_be_owing;

u_be_owing=time*rate_3; //$0.15 per minute

cout<<"You will be charged"<<"$"<<u_be_owing<<" "<<"for the call. \n";

}

cout <<endl;

cout << "Do another computation? (y/n)" << endl;

cin >> choice;

}while(choice == 'Y' || choice == 'y');

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

This isn't an asnwer to your question, as I've never touched C++, but I just thought if you're using .NET would it not make more sense to do something like

if ((day.ToUpper=='MO')

? Avoids having to check for every variation...

If you aren't using .NET, sorry to bother :p

Link to comment
Share on other sites

  • 0
This isn't an asnwer to your question, as I've never touched C++, but I just thought if you're using .NET would it not make more sense to do something like
if ((day.ToUpper=='MO')

? Avoids having to check for every variation...

If you aren't using .NET, sorry to bother :p

Nope no .NET. thanks anyway bro

Link to comment
Share on other sites

  • 0

No need to bump your thread every 20 minutes.

Use cin.getline() if you want to get a whole line of input (as is the case here, since the user ends his request by hitting Enter). Then convert the result to whatever you want, using, for instance, atoi().

Link to comment
Share on other sites

  • 0

You should change the char to string, because if i remember right, char only allows for 1 character, i was able to get past your problem just by typing in only 1 letter.

Link to comment
Share on other sites

  • 0
No need to bump your thread every 20 minutes.

Use cin.getline() if you want to get a whole line of input (as is the case here, since the user ends his request by hitting Enter). Then convert the result to whatever you want, using, for instance, atoi().

sorry this is just beginner problem an I have not worked up to learning string in my class. i believe i understand cin.getline through your link.. but could you post an example of conversion by atoi?

You should change the char to string, because if i remember right, char only allows for 1 character, i was able to get past your problem just by typing in only 1 letter.

which "char" are you referring to ? "char choice" or "char day".. i hav yet to learn string could you post how you solved the problem through using string?

Link to comment
Share on other sites

  • 0

Ok, if you can't use a string, then you can't input a string. In your example, you have entered "su", which is a string of two characters; obviously, it won't fit a char which is a single character, and that screws up the rest of the program.

What you could do is input a number for the day of week, 1-7 for example. Use an int instead of char. Char is for single characters only.

There's an example of atoi on the page I linked to in my previous post. For cin.getline() too.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.