0

我正在编写的程序是假设列出每个学生支付的所有款项,显示已支付和未付的金额。

但是问题是它由于某种我似乎找不到的原因而无法正确显示。

payment.txt 文件内容按以下顺序排列: Student Code 金额类型(空白为现金)

11  50
12  25  4543 2323 2321
12  25  Barclays
13  100 
14  100
15  50  4545 6343 4342
15  25  HSBC
16  100
17  100
18  100
19  100
20  25  4546 3432 3211
21  75
22  100 Lloyds
23  100

这是到目前为止的代码:

void payment()
{
    // Display message asking for the user input
    std::cout << "\nList all payment made by each student, show amount paid and outstanding." << std::endl;

    // Read from text file and Display list of payment

    std::ifstream infile;               // enable to open, read in and close a text file
    float StudentCode;                  // to store the student enrolment number
    float Amount;                       // to store the amount of money
    float Type;                         // to store information on type of payment made
    float Outstanding;                  // to store amount of money is due
    std::map<int, float> amountsPaid;   

    infile.open("Payment.txt");         // open a text file called Payment

    if (!infile)                 
    {
        std::cout << "List is empty" << std::endl;      // if the file is empty it output the message
    } 
    else 
    {
        // Display Headings and sub-headings
        std::cout << "\nList of Payment: " << std::endl;
        std::cout << "" << std::endl;
        std::cout << "Enrolment No." << "   " << "Amount" << "  " << "Outstanding" << std::endl;

        // accumulate amounts
        while (infile >> StudentCode >> Amount) 
        {
            amountsPaid[StudentCode] += Amount;
        }

        // loop through map and print all entries
        for (auto i = amountsPaid.begin(); i != amountsPaid.end(); ++i) 
        {
            float outstanding = 100 - i->second;
            // Display the list of payment made by each student
            std::cout << i->first << "      " << i->second << " " << "$: " << outstanding << '\n' << std::endl;
        }
    }

    infile.close();         // close the text file  
}

它在运行时显示以下内容:

11 50 $50 12 25 $75 2321 12 $88 4543 2323 $-2223

你能帮忙解释一下为什么这样做吗?谢谢

4

2 回答 2

2

The key section of code is here.

while (infile >> StudentCode >> Amount) 
{
    amountsPaid[StudentCode] += Amount;
}

This loop pulls of pairs of numbers. These are the pairs that get pulled off the stream:

11   50
12   25 
4543 2323 
2321 12

At that point the text Barclays is encountered and so the while loop terminates. That is because the text cannot be converted into a float.

To solve your problem you will need to switch to line oriented processing. Use getline() to pull off a line at a time. And then break the line into distinct items. One possible solution would be like so:

string line;
while (getline(infile, line))
{
    istringstream strm(line);
    strm >> StudentCode >> Amount;
    amountsPaid[StudentCode] += Amount;
}
于 2012-12-13T20:41:02.033 回答
1

您的文件包含的内容实际上不止两列,而且您只读取两列,因此下一个infile >> StudentCode >> Amount会将付款类型读取为 StudentCode。在阅读下一个“StudentCode >> Amount”组合之前,您应该在文件中只创建两列或丢弃更多列直到行尾,例如
while(infile >> StudentCode >> Amount)
{
    amountPaid[StudentCode] += Amount;
    infile.getline(buff, size); // 它会读取剩余的行
}

于 2012-12-13T20:43:05.370 回答