Question: My C + + code is not printing the 'Night call is free' value where applicable in the Minutes used so far column, it isn't

My C++ code is not printing the 'Night call is free' value where applicable in the Minutes used so far column, it isn't printing the correct values for the Cost column, and the Total cost of the bill is only displaying the basic monthly rate of $39.99. The code is below and a screenshot of the requirements is attached in the image.
#include
#include
#include
using namespace std;
// Constants
const double DAYTIME_RATE_WITHIN_AREA =0.29;
const double DAYTIME_RATE_LONG_DISTANCE =0.69;
const double BASIC_MONTHLY_RATE =39.99;
const int NIGHT_START_HOUR =18; //6pm
const int NIGHT_END_HOUR =6; //6am
const int FREE_MINUTES_DAYTIME =200;
// Structure to hold call information
struct Call {
int startHour;
int duration;
char type;
};
// Function to calculate the cost of a call
double calculateCallCost(Call call){
double cost =0.0;
if ((call.startHour >= NIGHT_START_HOUR && call.startHour =24)||(call.startHour >=0 && call.startHour NIGHT_END_HOUR)){
// Night time call
return cost; // Night calls are free
} else {
// Daytime call
int additionalMinutes = max(0, call.duration - FREE_MINUTES_DAYTIME);
if (additionalMinutes >0){
if (call.type =='A'){
cost = additionalMinutes * DAYTIME_RATE_WITHIN_AREA;
} else if (call.type =='L'){
cost = additionalMinutes * DAYTIME_RATE_LONG_DISTANCE;
}
}
return cost;
}
}
int main(){
ifstream inFile("phone_calls.txt");
if (!inFile){
cerr "Unable to open file";
return 1;
}
double totalBill = BASIC_MONTHLY_RATE;
int totalDaytimeMinutes =0;
Call call;
// Print header
cout "Cellphone charges - MP3 by (omitted my name)Skills needed: logical expressions, nested if-else statements
Problem:
the day or the night.
daytime call.
information about each call on a separate line.
This information is:
The time of day that the call started (using an int to represent a 24 hour clock).
The duration of the call in minutes (int), and
A char for the type of call - A for a call within the area, L for a long-distance call.
For example, the line
1830,20 A
means that the call started at 6:30pm, lasted for 20 minutes and was within the calling area.
SUGGESTION
You should design, compile, run and debug your program in stages.
You might start by testing if your program can just read and echo the data file.
After this is working accurately, write the code to determine whether the call is a day or night one.
Make sure you always submit(upload) the .cpp file(s) and the output file(s) or a screen shot of the output.
Sample output from the first few lines of the data follows:
Cellphone charges - MP3 by (your name goes here)
Time Duration Type of call Minutes used so far Cost" endl;
cout setw(5) "Time" setw(10) "Duration" setw(15) "Type of call" setw(20) "Minutes used so far" setw(10) "Cost" endl;
// Read call data and calculate costs
while (inFile >> call.startHour >> call.duration >> call.type){
cout setw(5) call.startHour setw(10) call.duration setw(15) call.type;
// Update totalDaytimeMinutes for daytime calls only
if ((call.startHour >= NIGHT_START_HOUR && call.startHour 24)||(call.startHour >=0 && call.startHour NIGHT_END_HOUR)){
if ((call.startHour + call.duration)>= NIGHT_END_HOUR && (call.startHour + call.duration) NIGHT_START_HOUR){
cout setw(20) "Night call is free";
} else {
totalDaytimeMinutes += call.duration;
cout setw(20) totalDaytimeMinutes;
}
} else {
totalDaytimeMinutes += min(call.duration, FREE_MINUTES_DAYTIME);
cout setw(20) totalDaytimeMinutes;
}
double callCost = calculateCallCost(call);
totalBill += callCost;
if (callCost >0){
cout setw(10) fixed setprecision(2) callCost endl;
} else {
cout setw(10)"$0.00" endl;
}
}
// Print total bill
cout "Total bill for the month: $" fixed setprecision(2) totalBill endl;
inFile.close();
return 0;
}
 My C++ code is not printing the 'Night call is free'

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!