变量 hey
应该在我通过 for 循环时被覆盖,所以如果我不输入 1,它会说“再见”,但是如果我在它通过循环时输入 1循环,它应该说“你好”,但它应该说“再见”。
#include <iostream>
using namespace std;
int main(){
char hey;
for (int n = 0; n <= 10; n++){
cout << "enter 1" << endl;
cin >> hey;
if (hey == 1)
cout << "hello\n";
else
cout << "bye";
}
}
最佳答案
ASCII 数字 49 的字符 '1'
与数字 1 不同。
使用
if (hey == '1')
如果你想将某物与字符 a 进行比较,你也必须使用 'a'
。这里也是一样。
关于c++ - 为什么我无法在循环中覆盖变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31039849/