所以我试图为计算器创建一个日志,以便可以返回并检查以确保所有输入的数字均正确输入。这是代码。
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream f;
f.open("pumpout.txt");
float number = 0;
float total = 0;
char operand;
bool running = true;
cin >> total;
f << total << " ";
cin >> operand;
f << operand << " ";
cin >> number;
f << number << " = ";
while (running = true) {
if (operand == '/') {
total = total / number;
cout << total << endl;
f << total << "\n" << total << " ";
cin >> operand;
}
else if (operand == '*') {
total = total * number;
cout << total << endl;
f << total << "\n" << total << " ";
cin >> operand;
}
else if (operand == '+') {
total = total + number;
cout << total << endl;
f << total << "\n" << total << " ";
cin >> operand;
}
else if (operand == '-') {
total = total - number;
cout << total << endl;
f << total << "\n" << total << " ";
cin >> operand;
}
f << operand << " ";
cin >> number;
f << number << " = ";
}
}
因此它会按照我想要的方式添加并执行所有操作,但不会转到文本文件。格式应为:
total operand # = total
通过整个文本文件。任何帮助都会很棒。
答案 0 :(得分:0)
我希望您看不到任何输出,因为循环永远不会停止,文件也永远不会关闭。如果您希望输出立即显示在文件中,则应使用std::flush
或std::endl
(例如, f << total << "\n" << total << " " << flush;
。
出于效率的考虑,文件输出被缓冲,这意味着文件输出首先被写入缓冲区,它不会立即出现在文件中。刷新是获取缓冲区并将其立即写入文件的过程。