修复我的代码的建议

时间:2015-06-07 22:14:58

标签: c++

我必须读取一个类似于

的输入文件calls_history.txt
Mo  13:30   16
Mo  8:15    35
Tu  7:50    20
We  17:45   30
Th  8:00    45
Su  23:50   30

然后计算每次通话的费用。 到目前为止,这就是我得到的。

#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;
}

我测试了它,只得到了这个结果

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.

任何建议,以便我可以显示所有其他行和数据? 我必须让它看起来像这样

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

1 个答案:

答案 0 :(得分:0)

问题是因为关闭文件的位置不正确。

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

你想:

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

但是你无论如何都不需要显式关闭文件。当inFile在函数末尾超出范围时,ifstream的析构函数将处理它。

您将遇到的下一个问题:

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

输入:

Mo  13:30   16

会发生什么:

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

没有测试和处理读取失败。未分配使用分钟开始和持续时间。接下来是漫画般的嘻哈。

尝试改为:

char colon;
if (iss >> day >> hour_start >> colon >> minute_start >> duration && (colon == ':'))
{
     do calculations
}
else
{
    notify user of bad file
}