-1

我在下面编码的程序应该列出每个学生支付的所有款项,显示已支付和未付的金额

我需要以下部分的帮助:

void payment()
{
    // Display message asking for the user input
    std::cout << "List 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

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

    if (!infile)                 
    {
        std::cout << "Item list is empty" << std::endl;     // if the file is empty it output the message
    } 
    else 
    {
        std::cout << "List of Payment: " << std::endl;
        std::cout << "" << std::endl;
        std::cout << "Enrolment No." << "Amount" << "Outstanding" << std::endl;

        // If there is Student Code that has the same number, it need to combined the amount it paid 
        // For an example 
        // Student Code: 12 Amount: 25
        // Student Code: 12 Amount: 50
        // so it should display the following when the program runs:
        // Student Code: 12 Amount: 75

        while(!infile.eof())            // output the description of the text file onto the screen
        {
            getline(infile,StudentCode,Amount);

            Outstanding = Amount - 100;

            std::cout << StudentCode << Amount << "$" << Outstanding << std::endl;
            //iter++;
        }
        std::cout << "End of list\n" << std::endl;
    }

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

getline 部分有什么问题:

getline(infile,StudentCode, Amount);

此外,该程序不应显示重复的学生代码,而是结合其支付的金额。我在评论部分解释的地方

// If there is Student Code that has the same number .....

我该怎么做呢?

4

3 回答 3

1

getline将流中的一行读入字符串。你想要做的,更像是这样

while (infile >> StudentCode >> Amount) {
    // process values
}

如果要汇总所有金额,则必须先累积,然后循环收集的值并打印它们

std::map<int, float> amountsPaid;
int studentCode;
float amount;
// 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 = i->second - 100;
    std::cout << i->first << ": " << i->second << "$, " << outstanding << '\n';
}
于 2012-12-11T22:13:00.120 回答
1

这里有几个问题。一种是getline将一行文本读入单个std::string变量,而不是多个float字段。

为此,您可以尝试

infile >> StudentCode >> Amount;

第二个问题是

while(!infile.eof())

不会检查下一个输入是否可以工作,但如果上一个输入尝试失败,因为它到达文件结尾。

标准方法是将这些组合成

while (infile >> StudentCode >> Amount)
{
    // do something with the code and amount
}
于 2012-12-11T22:16:17.713 回答
0

您对 getline 的调用似乎不正确。

该文件指出

istream& getline ( istream& is, string& str, char delim );

但是你给它

getline(istream&, float, float);

您应该尝试将该行作为字符串读取,然后解析出 2 个浮点数。

由于您使用的是 c++,如果文件格式正确,您可以重定向 cin,它会更容易。你可以做类似的事情

while (infile >> StudentCode) {
  infile >> Amount;
} 
于 2012-12-11T22:15:34.200 回答