Need help adding a loop.

So i have this program that calculates costs of long distance calls.

Regular rate for a call is 0.10 per minute
Any call started at or after 1800 hours(6pm) but before 0800hours(8am) is discount 50%
Any call longer than 60 minutes receives a 15% discount
All calls are subject to a 4% federal tax
I have the program running. Now i need to add a loop so that if the user enters an invalid start time or invalid call length, it will display a message and have the user start over. Remember, the user must enter start time ONLY in military time and Length of call ONLY in total minutes. Here is my program without the loop. Can someone help me add the loop?

#include<iostream>

using namespace std;

float computegrosscost(int, float);
float computenetcost(int, int, float);

const float rate = 0.10;
const float costdiscount = 0.50;
const float durationdiscount = 0.15;
const float federaltax = 0.04;
float grosscost;

int main()
{
int starttime;
int minutes;
float grosscost;
float netcost;

cout<< "Enter start time of call in military time: ";
cin>> starttime;
cout<< "Enter length of call in minutes: ";
cin>> minutes;

grosscost=computegrosscost(minutes, rate);
netcost=computenetcost(starttime, minutes, grosscost);

cout << "The gross cost for the call is "<< grosscost <<endl;
cout<< "The net cost for the call is "<<netcost <<endl;

system("pause");

return 0;
}

float computegrosscost(int minutestime, float cost)
{
float grosspay = minutestime * cost;
return grosspay;
}

float computenetcost( int starttime, int minutes, float grosscost)
{
float netcost = grosscost;
float discount = 0.0;

if((starttime >= 1800) || (starttime <= 800))
{
discount = netcost * costdiscount;
netcost = netcost - discount;
}

if (minutes > 60)
{
discount = netcost * durationdiscount;
netcost = netcost - discount;
}

return (netcost+federaltax);
}
Last edited on
http://www.cplusplus.com/doc/tutorial/control/

Use while or do-while.
Get input. Check whether it is valid. Let the loop terminate on good input (or break out).
I added my changes for the loop but still get errors. It says expected unqualified id and doesnt run. Am i placing my loop wrong? I just want it to make sure the start time is valid. If not then break and start over.

#include<iostream>

using namespace std;

int count;
float computegrosscost(int, float);
float computenetcost(int, int, float);
const float rate = 0.10;
const float costdiscount = 0.50;
const float durationdiscount = 0.15;
const float federaltax = 0.04;
float grosscost;
string prompt = "Enter start time of call in military time: ";

int main()

{
int starttime;
int minutes;
float grosscost;
float netcost;
cout<< "Enter start time of call in military time: ";
cin>> starttime;
cout<<
"Enter length of call in minutes: ";
cin>> minutes;
grosscost=computegrosscost(minutes, rate);
netcost=computenetcost(starttime, minutes, grosscost);
cout <<
"The gross cost for the call is "<< grosscost <<endl;
cout<<
"The net cost for the call is "<<netcost <<endl;
system("pause");
return 0;
}

while(true) //This error says "expected unqualified id"
{
if((starttime>0)&&(starttime<=2400))
break;
cout<< prompt;
cin>> starttime;
}

float computegrosscost(int minutestime, float cost)
{
float grosspay = minutestime * cost;
return grosspay;
}

float computenetcost( int starttime, int minutes, float grosscost)
{
float netcost = grosscost;
float discount = 0.0;
if((starttime >= 1800) || (starttime <= 800))
{
discount = netcost * costdiscount;
netcost = netcost - discount;
}
if (minutes > 60)
{
discount = netcost * durationdiscount;
netcost = netcost - discount;
}

return (netcost+federaltax);
}
If there is a better way of doing it please let me know.Thanks.
use code tags please.

your while loop is completely outside of your main() and your other functions.
you also need to #include<string>
Last edited on

It says expected unqualified id and doesnt run.


1
2
3
4
5
6
7
while(true) //This error says "expected unqualified id"
{
if((starttime>0)&&(starttime<=2400))
break;
cout<< prompt;
cin>> starttime;
}

The above code is not part of a function. You can not place code outside a function.

Here is a function that prompts for starttime. It doesn't return until a valid value is entered. Note that miliary time includes 00:00 but not 24:00. All responsibility for prompting, inputting and checking the value is in the function.
1
2
3
4
5
6
7
8
9
int get_starttime ()
{   int starttime;

    do  
    {   cout<< "Enter start time of call in military time: ";
        cin>> starttime;
    } while (! ((starttime>=0)&&(starttime<2400)));
    return starttime;
}


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/

Ok sorry about the code tag. I made my changes but its not allowing me to move past a point. I will include where the problem is.

[CODE]:
#include<iostream>
#include<string>
#include<cmath>

using namespace std;

int starttime;
float computegrosscost(int, float);
float computenetcost(int, int, float);
const float rate = 0.10;
const float costdiscount = 0.50;
const float durationdiscount = 0.15;
const float federaltax = 0.04;
float grosscost;
string prompt = "Enter start time of call in military time: ";

int main()
{
int starttime;
int minutes;
float grosscost;
float netcost;

// If start time is not in 2400 hr time frame. You must start over.
{ if((starttime>0)&&(starttime<=2400))
do
{
cout<< "Enter start time of call in military time: ";
cin>> starttime;
}
while(!((starttime>=0)&&(starttime<=2400)));
return starttime;
}
//The error im getting says "code will never be executed" and its underling cout.
cout <<"Enter length of call in minutes: ";
cin>> minutes;

//Compute gross cost
grosscost=computegrosscost(minutes, rate);

//Compute net cost
netcost=computenetcost(starttime, minutes, grosscost);

cout <<"The gross cost for the call is "<< grosscost <<endl;
cout<<"The net cost for the call is "<<netcost <<endl;

system("pause");
return 0;
}

//This function definition calculates gross cost
float computegrosscost(int minutestime, float cost)

{
float grosspay = minutestime * cost;
return grosspay;
}

//This function calculates net cost
float computenetcost( int starttime, int minutes, float grosscost)

{
float netcost = grosscost;
float discount = 0.0;

//If start time is after 6pm and before 8am then 50% discount
if((starttime >= 1800) || (starttime <= 800))
{
discount = netcost * costdiscount;
netcost = netcost - discount;
}

//If call time is greater than 60 minutes you get 15% discount
if (minutes > 60)
{
discount = netcost * durationdiscount;
netcost = netcost - discount;
}

//Compute with federal tax
return (netcost+federaltax);
}
Your code tags did not work. Use the word code in lower case and be sure to include the /code tag at the end.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{   int starttime;
    int minutes;
    float grosscost;
    float netcost;

    // If start time is not in 2400 hr time frame. You must start over.
    { if((starttime>0)&&(starttime<=2400))
    do
    {   cout<< "Enter start time of call in military time: ";
         cin>> starttime;
    }
    while(!((starttime>=0)&&(starttime<=2400)));
    return starttime;
}

The if statement at line 8 is unnecessary.
The return statement at line 14 will cause you to exit out of the program and you will never reach the next line.


Ok thanks i fixed it. I just have one more question.How would you include a break so that it starts over?
This is what i got so far. It still wont loop and break out. I tried 2500 as a start time and it keeps going.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<string>

using namespace std;

int starttime;
float computegrosscost(int, float);
float computenetcost(int, int, float);
const float rate = 0.10;
const float costdiscount = 0.50;
const float durationdiscount = 0.15;
const float federaltax = 0.04;
float grosscost;
string prompt = "Enter start time of call in military time: ";

int main()
{
    int starttime;
    int minutes;
    float grosscost;
    float netcost;
    
    do{
    cout<< "Enter start time of call in military time: ";
    cin>> starttime;
    }
    while((starttime>0)&&(starttime<2400));
    
    cout << "Enter length of call in minutes: "<<endl;
    cin>> minutes;
       
    //Compute gross cost
    grosscost=computegrosscost(minutes, rate);
    
    //Compute net cost
    netcost=computenetcost(starttime, minutes, grosscost);
    
    cout <<"The gross cost for the call is "<< grosscost <<endl;
    cout<<"The net cost for the call is "<<netcost <<endl;
    
    system("pause");
    return 0;
}


//This function definition calculates gross cost
float computegrosscost(int minutestime, float cost)
{
    float grosspay = minutestime * cost;
    return grosspay;
}

//This function calculates net cost
float computenetcost( int starttime, int minutes, float grosscost)
{
    float netcost = grosscost;
    float discount = 0.0;
    
    //If start time is after 6pm and before 8am then 50% discount
    if((starttime >= 1800) || (starttime <= 800))
    {
        discount = netcost * costdiscount;
        netcost = netcost - discount;
    }
    
    //If call time is greater than 60 minutes you get 15% discount
    if (minutes > 60)
    {
        discount = netcost * durationdiscount;
        netcost = netcost - discount;
}
    //Compute with federal tax
   return (netcost+federaltax);
}

line 27: You're missing a not condition. You want to loop until the condition is not true.

BTW, as I mentioned earlier, 00:00 is a valid military time. 24:00 is not.
Thanks guys. I got it working wonderfully. Appreciate the help.
Topic archived. No new replies allowed.