#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
usingnamespace std;
int main()
{
ifstream inFile;
string filename, day, Mo, Tu, We, Th, Fr, Sa, Su;
int duration;
string time_start;
int hour_start, minute_start;
double TotalCost, cost_1, cost_2;
cout << "Enter filename: ";
cin >> filename;
cout << "Day" << "\tTime" << "\tDuration" << "\tCost" << endl;
cout << "----------------------------------------------" << endl;
inFile.open (filename.c_str());
string line;
if (inFile)
{
//read records from file
while (getline (inFile, line))
{
stringstream iss(line);
//split into 4 fields
while(iss)
{
iss >> day;
iss >> hour_start;
iss >> minute_start;
iss >> duration;
}
if (day == "Mo" || day == "Tu" || day == "We" || day == "Th" || day == "Fr")
{
if (hour_start >= 8 && hour_start <= 18)
{
TotalCost = duration*0.40;
}
if ((hour_start = 7, minute_start < 60) && (minute_start+duration >= 60))
{
cost_1 = (60-minute_start)*0.25;
cost_2 = ((duration-(60-minute_start))*0.40);
TotalCost = cost_1+cost_2;
}
if (hour_start < 8 && hour_start >= 18)
{
TotalCost = duration*0.25;
}
}
if (day == "Sa" || day == "Su")
{
if ((hour_start =23 && minute_start <60)&& (minute_start+duration >=60))
{
cost_1 = (60-minute_start)*0.15;
cost_2 = ((duration-(60-minute_start))*0.25);
TotalCost = cost_1+cost_2;
}
else
{
TotalCost = duration*0.15;
}
}
cout << day << "\t" << hour_start <<":" << minute_start << "\t" << duration << "\t\t$";
cout << setprecision(2) << fixed << TotalCost << endl;
inFile.close();
}
}
return 0;
}
I'm testing it out and I only have this as my result.
Enter filename: calls_history.txt
Day Time Duration Cost
----------------------------------------------
Mo 7:30 16 $6.40
Process returned 0 (0x0) execution time : 3.622 s
Press any key to continue.
Any suggestion so that I can show all the other datas?
By moving that down two lines, he meant to move it between lines 78 and 79 in your code snippet. In the original code, you close the file at the end of the body of the loop, causing the condition governing the loop to be false.
Proper indentation would probably have helped you discover this.
Ok I fixed it and it worked but now there's a problem with the hour_start. The calculations are all correct but the time is weird now.
Enter filename: calls_history.txt
Day Time Duration Cost
----------------------------------------------
Mo 7:30 16 $6.40
Mo 7:15 35 $14.00
Tu 7:50 20 $6.50
We 7:45 30 $9.75
Th 7:0 45 $18.00
Su 1:50 30 $6.50
Process returned 0 (0x0) execution time : 3.903 s
Press any key to continue.
Should be == to compare values not =. Same for the other if statements with hour_start.
Unless this is intended with your use of commas in if statements, but it doesn't seem like it.
Enter filename: calls_history.txt
Day Time Duration Cost
----------------------------------------------
Mo 13:30 16 $6.40
Mo 8:15 35 $14.00
Tu 7:50 20 $6.50
We 17:45 30 $9.75
Th 8:0 45 $18.00
Su 23:50 30 $6.50
Process returned 0 (0x0) execution time : 3.974 s
Press any key to continue.
How come on Thursday the time is only 8:0 when it should be 8:00 though?