【发布时间】:2021-11-20 13:41:22
【问题描述】:
#include <iostream>
using namespace std;
int x, input1, input2;
string repeat;
int main() {
while (repeat.compare("n") != 0) {
cout << "input1 : ";
cin >> input1; //input
cout << "repeat?(y/n) ";
cin >> repeat;
}
cout << input1; //output
}
如果我运行上面的代码,例如输入 1 y 7 y 5 n,代码的输出只会出现我输入的最后一个数字,我如何让它们全部打印在 cout 中?不要告诉我用不同的名称创建新变量。
【问题讨论】:
-
我正在更改问题,抱歉
-
读入后可以有
vector<string> inputs;,然后inputs.push_back(input1);,然后for (auto&& s : inputs) cout << s << "\n";全部输出。 -
整数替换为
vector<int> inputs;。 -
这并没有解决问题,但
while (repeat.compare("n") != 0)确实应该写成while (repeat != "n")。