下面的程序我的代码应该 列出了每个学生的所有付款,显示支付金额和优秀函数getline()和读取数据从文本文件
我需要一些帮助的以下部分:
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 .....
如何做到这一点解释?
的函数getline部分是相当容易地通过使用Google的API回答: http://msdn.microsoft.com/en-us/library/2whx1zkx( v = vs.71).aspx getline读取字符,而不是浮动。 – HerrJoebob
做**不**使用'eof()'来检测阅读是否完成:它没有**这样工作。相反,在**阅读之后,**总是**检查**,您尝试读取输入是正确的。你从哪里得到了'eof()'应该被使用的想法?如果是一本书,请告诉我们标题;如果是你的老师,请告诉你的教导停止教这个废话! –
当我在以前使用getline时,出于某种原因使用eof()。感谢您告诉我 – user1582575