所以如果我有一个像这样的简单交互式程序:
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#define cout os
int main() {
stringstream os;
cout << "If you would like to continue, type 'Continue'" << '\n';
string line;
while (cin >> line) {
if (line == "Continue") {
cout << "If you would like to continue, type 'Continue'" << '\n';
}
else { break; }
}
cout << "Program ended." << '\n';
cout << os.str();
return 0;
}
我如何使它能够包含我的指令“#define”,以便打印到标准输出的所有行都将在程序末尾由 cout << os.str() 打印,当这样做时它还会将最终的“cout”变成“os”吗?我已经尝试在最后使用 printf 而不是 os,并且遇到了麻烦/编译器错误,说“没有对 printf 的匹配函数调用”。
我希望我的问题是有道理的,如果这个问题已经被问过,但我在这里找不到它,我深表歉意。