Goals: Developing problem-solving skills, declaring variables, multi-way branche
ID: 3530757 • Letter: G
Question
Goals: Developing problem-solving skills, declaring variables, multi-way branches, data validation. Problem: 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 7:00 A.M. and 9:00 P.M., Monday through Friday, is billed at a rate of $0.45 per minute. b. Any call started before 7:00 A.M. or after 9:00 P.M., Monday through Friday, is billed at a rate of $0.20 per minute. c. Any call started on a Saturday or Sunday is billed 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. Input will come from the input fileExplanation / Answer
please rate - thanks
I went through with a fine tooth comb--does not have any variable defined more than 1 time
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const double mid_week_daytime = 0.45;
const double mid_week_night = 0.2;
const double weekend = 0.15;
int main ()
{ ifstream infile;
ofstream outfile;
infile.open("Project_3_input.txt");
outfile.open("Project_3_output.txt");
char d1,d2;
int number_of_the_day;
int time_period,time_min;
int duration;
int error ;
double total_cost;
if (infile.fail())
{cout << "An error is occuring when opening file" << endl;
}
else
{ infile >> d1>>d2;
while(infile)
{
error=0;
d1=toupper(d1);
d2=toupper(d2);
if((d1=='M')&&d2=='O')
{number_of_the_day = 1;
}
else if((d1=='T') && (d2=='U'))
{number_of_the_day = 1;
}
else if((d1=='W') && (d2=='E'))
{number_of_the_day = 1;
}
else if((d1=='T')&& (d2=='H'))
{number_of_the_day = 1;
}
else if((d1=='F') && (d2=='R'))
{number_of_the_day = 1;
}
else if((d1=='S') && (d2=='A'))
{number_of_the_day = 2;
}
else if((d1=='S') && (d2=='U'))
{number_of_the_day = 2;
}
else
{error=1;
infile.ignore(80,' ');
}
if (error==0)
{infile>>time_period>>time_min >> duration;
if (number_of_the_day == 1)
{if((time_period >= 7) && (time_period <= 21))
{total_cost = duration * mid_week_daytime;
}
else
total_cost = duration * mid_week_night;
}
else
total_cost = duration * weekend;
outfile<< total_cost << endl;
}
infile >> d1>>d2;
}
}
infile.close();
outfile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.