我正在做一个简单的任务,我在输出预期的输出时遇到了麻烦。有人可以帮我找出解决问题的方向吗?我们不允许使用istringstream。
else
{
upData >> houseIdent;
while(upData)
{
do{
upData >> vehType >> plate >> year >> vehPrice;
calculate = calcReg(vehType, plate, year,
vehPrice);
outDataOne << fixed << showpoint << setprecision(2);
outDataOne << ' ' << vehType << ' ' << plate << ' ' << calculate
<< endl;
ch = upData.peek();
}while (upData && (ch != '\n'));
upData >> houseIdent;
}
upData是ifstream .txt,outDataOne是ofstream .txt。输入类似于此
11111 M DKC294 2007 23001 A VID392 2010 10390
22222 A DKS382 2011 20390
33333 A DKF329 2001 30920 M AJD302 2004 15509
我继续得到的输出是前两个输出线是正确的但是第三行不返回并读取第二行ID号,但是将vehType设为2时保持11111作为ID号因为我对C ++很陌生,所以我对此非常困惑。
提前致谢
答案 0 :(得分:0)
好吧,我拿了你的代码并稍微修改了一下,所以我可以运行它:
#include <iostream>
#include <iomanip>
#include <string>
int main(int argc, char *argv[]) {
#define upData std::cin
#define outDataOne std::cout
string houseIdent;
char vehType;
string plate;
int year;
int vehPrice;
int ch;
upData >> houseIdent;
while (upData) {
do {
upData >> vehType >> plate >> year >> vehPrice;
auto calculate = 237237; // calcReg(vehType, plate, year, vehPrice);
outDataOne << std::fixed << std::showpoint << std::setprecision(2);
outDataOne << ' ' << vehType << ' ' << plate << ' ' << calculate << endl;
ch = upData.peek();
} while (upData && (ch != '\n'));
upData >> houseIdent;
}
return 0;
}
并输入你的意见:
11111 M DKC294 2007 23001 A VID392 2010 10390 22222 A DKS382 2011 20390 33333 A DKF329 2001 30920 M AJD302 2004 15509
它吐了出来:
M DKC294 237237 一个VID392 237237 一架DKS382 237237 一架DKF329 237237 M AJD302 237237
这似乎正在做正确的事情。这似乎留下了比答案更多的问题,但是:
1)注意,当你调用peek()时,它返回一个int,而不是一个char,因为结果可能是一个EOF。您没有显示任何变量的声明,但这可能是一个小问题。
2)在“update&gt;&gt; vehType ...”行后,您不会检查输入是否用完。您确实需要这样做,因为在您询问它无法提供的数据之前,流不会报告EOF。正如代码所示,看起来它可能会在最后一次循环中使用陈旧数据。
3)你说vehicleType报告为2而盘子是0390;这可能意味着它将“20390”解释为vehType的一个字符和其余字符串的组合。我不知道这是怎么发生的(当我尝试它时它没有发生),但我的猜测是你的输入中有一个隐藏的垃圾字符,它会丢掉东西。 (也许是一个unicode不间断的空间或某些?)这将有助于添加一个调试行,您可以打印出所读取的所有vehType / plate / year / vehPrice值,当它们被读入时,到看看代码出错的地方。