Suggestions on how to fix my code? Please I'm already stressed enough

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.

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


So far, this is what I got.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#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 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?
Last edited on
Try moving
inFile.close();
down 2 lines

Maybe just one line but since I didn't test it I'm thinking 2 will do it for sure.

When one line works and the rest do not, you have a logic problem, start with where you open and close the file and zoom in from there.
Last edited on
both results are the same :(
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.
if ((hour_start = 7, minute_start < 60) && (minute_start+duration >= 60))

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.
Last edited on
closed account (48T7M4Gy)
Looks very much like the file being closed too early just as Sam says.

Can test this easily by commenting out line 77. Also shows the importance of clearly formatting and indenting code.
Thanks you guys, now I have this
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?
Last edited on
closed account (48T7M4Gy)
The number after the ':' is defined by you as an int so if you want 0 to output as 00 you have to format the number with printf() or other means.
Perhaps the following code will give you an idea or two:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// http://ideone.com/YzC3La
#include <iomanip>
#include <iostream>
#include <string>

int main()
{
    const unsigned width = 15;
    const unsigned num_width = 2;
    const unsigned string_width = width - num_width;

    for (unsigned i = 0; i < width-num_width-1; ++i)
    {
        std::cout << std::setfill('0') << std::setw(num_width) << i;
        std::cout << std::setfill(' ') << std::setw(string_width) << std::string(i+1, '*') << '\n';
    }
}
Thank you everyone for helping me out.
Topic archived. No new replies allowed.