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.
Program:
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int main ()
{
int minutes, startTime;
char ch;
string Day;
double cost, rate;
cout << fixed << showpoint << setprecision (2);
do
{
cout << "Enter start time of the call(For
example, 2:30 = 2330): ";
cin >> startTime;
while(startTime < 0 || startTime >= 2400)
{
cout << "\nInvalid time.";
cout << "Enter start time of the call(For
example, 2:30 = 2330): ";
cin >> startTime;
}
cout << "Enter length of the call in minutes: ";
cin >> minutes;
cout << "Enter the day of the week: ";
cin >> Day;
if(Day == "monday"|| Day == "MONDAY"
|| Day == "tuesday" || Day == "TUESDAY"
|| Day =="wednesday" || Day == "WEDNESDAY"
|| Day =="THURSDAY" || Day == "thursday"
|| Day == "friday" || Day =="FRIDAY")
{
if (startTime >= 800 && startTime <= 1800)
rate = 0.4;
else
rate = 0.25;
cost = minutes * rate;
cout << "\nRate for the call was " << "$"
<< rate << " a minute"<< endl
<< "Your total cost: " << "$"
<< cost << endl;
}
else if(Day =="saturday" || Day =="SATURDAY"
|| Day =="sunday" || Day =="SUNDAY")
{
rate = 0.15;
cost = minutes * rate;
cout << "\Rate for the call was " << "$"
<< rate << " a minute"<< endl
<< "Your total cost: " << "$" << cost;
}
else
cout << "\nInvalid.";
cout << endl << "\nWould you like to calculate
your bill again? (y/n): ";
cin >> ch;
cout << endl << endl;
}
while( ch =='Y' || ch == 'y');
cout << "\nEnd of Program\n\n";
return 0;
}
Output:
Enter start time of the call(For example, 2:30 = 2330): 1330
Enter length of the call in minutes: 110
Enter the day of the week: monday
Rate for the call was $0.40 a minute
Your total cost: $44.00
Would you like to calculate your bill again? (y/n): y
Enter start time of the call(For example, 2:30 = 2330): 5000
Invalid time.Enter start time of the call(For example, 2:30 = 2330): 1000
Enter length of the call in minutes: 30
Enter the day of the week: FRIDAY
Rate for the call was $0.40 a minute
Your total cost: $12.00
Would you like to calculate your bill again? (y/n): n
End of Program