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?
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;
}
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/
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;
//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.