下面是我编写的代码片段。我没有包括所有的代码从我的功能,因为我没有它在我的工作计算机在我的工作计算机。我需要循环大约10行数据,收集信息,然后执行计算(煅烧数据)并输出到输出文本文件(senddata)。我的函数似乎工作得很完美,但它们没有读过我的文本文档中的第一行。我能读第一行,计算第一行,然后输出第一行。
/*
My input file is:
10 0 S Y 100
5 7 S N 50
20 4 D Y 9
11 2 S Y 6
5 1 S N 120
31 5 S N 500
15 3 D N 40
18 4 S N 50
12 0 S N 40
26 7 D Y 200
*/
void getdata (int & adultget, int & childget, char & mealtypeget, char & weekendget, int & depositget, bool & error)
ifstream infile;
ofstream outfile;
int main ()
{
infile.open("C:\\input.txt");
outfile.open("C:\\output.txt");
while (infile)
{
getdata(adult, child, mealtype, weekend, deposit, error);
calcdata(adult, child, mealtype, weekend, deposit, adultcost, childcost, totalfood, surcharge, tax, tip, totalparty, discount, totaldue);
senddata(adultcost, childcost, mealtype, weekend, deposit);
}
infile.close();
outfile.close();
return 0;
}
void getdata (int & adultget, int & childget, char & mealtypeget, char & weekendget, int & depositget, bool & error)
{
infile >> adultget >> childget >> mealtypeget >> weekendget >> depositget;
.
.
.
}我的输入文件大约有10行数据,混合了int和char。我的函数只是读取文件的第一行。有什么帮助吗?
发布于 2013-11-14 18:51:53
你应该在正确的地方检查溪流。更改getdata,它返回一个bool
bool getdata (...)
{
bool ok = infile >> adultget >> ...;
...
return ok;
}循环应该是:
while (getdata(adult, child, ... ))
{
calcdata(adult, child, ...);
senddata(adultcost, childcost, ...);
}https://stackoverflow.com/questions/19985681
复制相似问题