Write a C++ program that computes the cost of a long-distance call. The cost of
ID: 3867521 • Letter: W
Question
Write a C++ 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 (must be the following format:)
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.
Format your output to two decimal places.
Define the following functions (value returning functions, Cally-by-Value):
validateUserInputTime(): validates the user's time input (hours and minutes should be positive numbers only including zero), and returns TRUE or FALSE.
validateUserInputDay(): validates the user's day input (only characters are allowed: Mo Tu We Th Fr Sa Su), and returns TRUE or FALSE. Allow uppercase and lowercase input.
validateUserInputCallLength(): validates the user's call length input (only positive integers are allowed), and returns TRUE or FALSE.
calculateTotalCost(): calculate the total cost of a call, and returns the total cost.
***Collect the user's inputs in the main() and pass them to the other functions.
***Allow the user to repeat the program.
***No object-oriented programming.
OUTPUTS:
**************************************************************************************
Enter the time the call starts in 24-hour rotation:
-13:10
Invalid time input.
Please try again.
Enter the time the call starts in 24-hour rotation:
**************************************************************************************
Enter the time the call starts in 24-hour rotation:
a:77
Invalid time input.
Please try again.
Enter the time the call starts in 24-hour rotation:
**************************************************************************************
Enter the time the call starts in 24-hour rotation:
13:10
Enter the first two letters of the day of the week:
ss
Invalid day input.
Please try again.
Enter the first two letters of the day of the week:
**************************************************************************************
Enter the time the call starts in 24-hour rotation:
13:10
Enter the first two letters of the day of the week:
1s
Invalid day input.
Please try again.
Enter the first two letters of the day of the week:
**************************************************************************************
Enter the time the call starts in 24-hour rotation:
13:10
Enter the first two letters of the day of the week:
Mo
Enter the length of the call in minutes:
-10
Invalid minute input.
Please try again.
Enter the length of the call in minutes:
**************************************************************************************
Enter the time the call starts in 24-hour rotation:
13:10
Enter the first two letters of the day of the week:
Mo
Enter the length of the call in minutes:
a1
Invalid minute input.
Please try again.
Enter the length of the call in minutes:
**************************************************************************************
Enter the time the call starts in 24-hour rotation:
13:10
Enter the first two letters of the day of the week:
Mo
Enter the length of the call in minutes:
10
Cost of the call: $4.00
Do you want to repeat the program?
y
**************************************************************************************
Enter the time the call starts in 24-hour rotation:
20:10
Enter the first two letters of the day of the week:
Fr
Enter the length of the call in minutes:
10
Cost of the call: $2.50
Do you want to repeat the program?
y
**************************************************************************************
Enter the time the call starts in 24-hour rotation:
10:10
Enter the first two letters of the day of the week:
Su
Enter the length of the call in minutes:
10
Cost of the call: $1.50
Do you want to repeat the program?
n
**************************************************************************************
Explanation / Answer
//////////////////////////callcost.cpp///////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string.h>
using namespace std;
///////////////////////function declarations//////////////////////
bool validateUserInputTime(int,char,int);
bool validateUserInputDay(string);
bool validateUserInputCallLength(int);
float calculateTotalCost(int,int,string,int);
string days[]={"Mo","Tu","We","Th","Fr","Sa","Su"};
float cost,fcost;
int main(int argc, char* argv[])
{
/////////////////////varible declaration //////////////////////
int t1,t2,duration;
char c,loopvarible='y';
string day;
bool result;
///////////////////loop for repeating/////////////////////
while(loopvarible=='y')
{
cout<<"Enter the time the call starts in 24-hour rotation: "<<endl;
cin>>t1;cin>>c;cin>>t2;
result=validateUserInputTime(t1,c,t2);
if(result==0)
{
cout << "Invalid time input."<<endl<<"Please try again.";
continue;
}
cout<<"Enter the first two letters of the day of the week:";
cin>>day;
result=validateUserInputDay(day);
if(result==0)
{
cout << "Invalid day input."<<endl<<"Please try again.";
continue;
}
cout<<"Enter the length of the call in minutes:"<<endl;
cin>>duration;
result=validateUserInputCallLength(duration);
if(result==0)
{
cout << "Invalid minute input."<<endl<<"Please try again.";
continue;
}
//////////////calling functon to find call cost///////////////
fcost= calculateTotalCost(t1,t2,day,duration);
cout<<"Cost of the call: $" << fcost<<endl;
cout<<"Do you want to repeat the program?";
cin>>loopvarible;
}
return 0;
}
////////////////////////checking time format is correct or not ////////////////////
bool validateUserInputTime(int time1,char ch,int time2)
{
if(time1<0)
return false;
if(time2<0)
return false;
if(ch!=':')
return false;
return true;
}
///////////////////////////checking day is valid or not///////////////
bool validateUserInputDay(string s)
{
int flag=0;
for(int i = 0; i < 7; i++){
if(days[i] == s){
flag=1;
}
}
if(flag==1)
return true;
else
return false;
}
/////////////////////function defination for input call lentgth validation//////////////
bool validateUserInputCallLength(int duration2)
{
if(duration2<0)
return false;
else
return true;
}
////////////////////function defination for findign cost/////////////////
float calculateTotalCost(int tm1,int tm2,string d,int duration3)
{
if((d=="Sa")||(d=="Su"))
{
cost=0.15*duration3;
}
else
{
if((tm1>=8)&&(tm2<18))
{
cost=0.40*duration3;
}
else
cost=0.25*duration3;
}
return cost;
}
output :
Enter the time the call starts in 24-hour rotation:
07:10
Enter the first two letters of the day of the week:Su
Enter the length of the call in minutes:
2
Cost of the call: $0.3
Do you want to repeat the program?y
Enter the time the call starts in 24-hour rotation:
12:12
Enter the first two letters of the day of the week:Tu
Enter the length of the call in minutes:
10
Cost of the call: $4
Do you want to repeat the program?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.