我正在做一个银行程序,在我的存款功能中,我有以下代码,它从文本文件中读取并将金额存储到 famount 中。唯一的问题是,当我运行程序并输出 famount 时,前面的行与上面的行具有完全相同的数据。
这是一段代码。
file>>firstname>>lastname;
cout<<endl<<firstname<<" "<<lastname<<endl;
string line;
while (getline(file, line))
{
//stringstream the getline for line string in file
istringstream iss(line);
file>>date>>amount;
iss >> date >> amount;
cout<<date<<"\t\t"<<amount<<endl;
famount+=amount;
// I tried to use this to stop reading after
// to the file ends but it still reads the last
// data on the file twice.
if(file.eof()){
break;
}
}
cout<<famount;
文本文件如下所示:
托尼·加迪斯
12 年 5 月 24 日 100
12 年 5 月 30 日 300
2012 年 7 月 1 日 -300
//控制台输出看起来像这样
托尼·加迪斯
12 年 5 月 24 日 100
12 年 5 月 30 日 300
2012 年 7 月 1 日 -300
07/01/12 -300 //这不应该在这里!!!!!!
-200 //应该是100
我能做些什么来纠正这个问题,为什么会这样。提前致谢。