我正在做一个银行业务程序和我的存款功能我有下面的代码,它从文本文件中读取并将数量存储到famount中。 唯一的问题是,当我运行该程序并输出了命令时,先前的行与上面的行具有完全相同的数据。C++如何阻止我的循环吐出最后一次输入两次
这是一段代码。
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;
文本文件看起来像这样:
托尼·盖迪斯
05/24/12 100
05/30/12 300
07/01/12 - 300
//控制台输出看起来像这样
个托尼·盖迪斯
05/24/12 100
05/30/12 300
07/01/12 -300
07/01/12 -300 //这不应该在这里!!!!!
-200 //应该导致100
我能做些什么来纠正这一点,为什么它的发生。 在此先感谢。
“咕哝咕哝两次” - >'EOF()'滥用。不要使用'eof()'。这个不成立。请检查您的输入操作。 –
'文件>>日期>>金额; iss >> date >> amount;'你从文件中提取两次,从stringstream中提取一次。为什么? – jrok
我仍然在试着看这个代码如何用那个输入生成那个输出。 ..我仍然没有看到它。如果有什么,我希望它输出*少*数据比你想要的,没有更多的,没有其他的具体原因,除了多余的'文件>>日期>>数量* *内循环,你明显忽略删除时添加每在线处理。等等...现在我明白了。如果 – WhozCraig