我有一个用C++编写的学校项目,我们必须解决一些错误,并在出现错误时退出程序。基本上,在else语句中,当表达式评估为false时,就像它不会向文件写入错误。如果我输出到控制台(通过cout),而不是写入文件它工作得很好,但是当我尝试写入输出文件时,它不起作用。基本上,这是我的问题。我的教授要求所有输出到文件中,这就是为什么我不能使用cout。那为什么它会把它输出到控制台,而不是文件?程序不会在exit()函数之前打印行吗?
P.S.我正在输出其他东西到文件,它工作正常,所以对于记录,我认为它被缩小到else语句中的小块代码。
if(tempUnit == 'C' || tempUnit == 'F')
{
if(tempUnit == 'F')
{
temp = convertTemp(temp, tempUnit);
}
temps[a] = temp;
printTemp(outputFile, tempUnit, temp);
printTimestamp(outputFile, humanDate);
outputFile << endl;
}
else{
// this is where it doesnt work
outputFile << "Error. Incorrect temperature unit, must be " <<
"either a capital C or a capital F. Program ended.";
exit(0);
}
尝试刷新文件在退出'outputFile << std :: flush;' – Erbureth