-1

I have to read an input file calls_history.txt that is like this

Mo  13:30   16
Mo  8:15    35
Tu  7:50    20
We  17:45   30
Th  8:00    45
Su  23:50   30

And then calculate the cost of each calls. So far, this is what I got.

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>

using namespace 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 3 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 tested it out and only got this 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 lines and datas? I have to make it look like this

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:00 45         $18.00
Su 23:50 30         $6.50
Total                         $61.15
Aki12
  • 1
  • 2

1 Answers1

0

The problem is because of bad placement of the file closing.

while (getline(inFile, line))
{
    ...parsing...
    inFile.close();
}

You want:

while (getline(inFile, line))
{
    ...parsing...
}
inFile.close();

But you don't need to explicitly close the file anyway. ifstreams destructor will take care of it when inFile goes out of scope at the end of the function.

The next problem you'll run into:

iss >> day; // read stream up to whitespace into string
iss >> hour_start; // read stream until not int into int
iss >> minute_start; // read stream until not int into int
iss >> duration; // read stream until not int into int

The input:

Mo  13:30   16

What happens:

iss >> day; // reads "Mo"
iss >> hour_start; // reads 13
iss >> minute_start; // tries to read ':' as int, fails
iss >> duration; // previous error not cleared. Fails

The read failures were not tested for and handled. Minute start and duration are used unassigned. Comic hi-jinks ensue.

Try instead:

char colon;
if (iss >> day >> hour_start >> colon >> minute_start >> duration && (colon == ':'))
{
     do calculations
}
else
{
    notify user of bad file
}
user4581301
  • 33,082
  • 7
  • 33
  • 54