C++ getline()和从文本文件读取数据
我在下面编写的程序应该 列出每个学生支付的所有款项,显示已支付金额和未支付金额 我需要以下部分的帮助:C++ getline()和从文本文件读取数据,c++,C++,我在下面编写的程序应该 列出每个学生支付的所有款项,显示已支付金额和未支付金额 我需要以下部分的帮助: 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 f
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
}
此外,该计划不应显示重复的学生代码,而应结合其支付的金额。
我在评论部分解释
// If there is Student Code that has the same number .....
如何执行此操作?将流中的一行读取为字符串。你想做的,更像这样
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';
}
std::map amountsPaid;
int学生代码;
浮动金额;
//累积金额
而(填充>>学生代码>>金额){
amountsPaid[studentCode]+=金额;
}
//循环浏览地图并打印所有条目
对于(自动i=amountsPaid.begin();i!=amountsPaid.end();++i){
浮动未完成=i->秒-100;
std::cout first您对getline的调用似乎不正确
文件说明
istream& getline ( istream& is, string& str, char delim );
但是你的付出
getline(istream&, float, float);
您应该尝试将该行作为字符串读取,然后解析出2个浮点数
既然你使用C++,如果文件格式化好,你可以重定向CIN,这样就更容易了。你可以做一些类似于
的事情。
while (infile >> StudentCode) {
infile >> Amount;
}
这里有几个问题。一个是getline
将一行文本读入单个std::string
变量,而不是多个float
字段
你可以试试看
infile >> StudentCode >> Amount;
第二个问题是
while(!infile.eof())
不会检查下一个输入是否正常工作,但如果上一个输入尝试失败,因为它已到达文件末尾
标准方法是将这些组合成
while (infile >> StudentCode >> Amount)
{
// do something with the code and amount
}
通过谷歌搜索API可以很容易地回答getline部分:getline读取字符,而不是浮点数。不要使用eof()
来检测读取是否完成:它不会以这种方式工作。相反,在读取后,始终检查您读取输入的尝试是否正确。您从中得到的想法是什么
应该使用吗?如果是一本书,请告诉我们书名;如果是你的老师写的,请告诉你的老师不要再教这些废话了!过去我使用getline时,我使用eof()出于某些原因。感谢您告诉meCan关于该计划的任何帮助都不应显示重复的学生代码,而应合并其支付的金额。如果有相同编号的学生代码,则需要合并其支付的金额,例如学生代码:12金额:25学生代码:12金额:50,因此应显示以下内容程序运行时:学生代码:12金额:75,而不是重复both@user1582575请参阅修改后的答案。首先收集值,然后打印所有值。
while (infile >> StudentCode >> Amount)
{
// do something with the code and amount
}