C++ 如果在 if 为 true 之后运行,为什么还会这样做
C++ Why does this else if run after the if is true
using namespace std;
int main(){
int userinp=-1,dig1=0,dig2=0,dig3=0,dig4=0;
while(userinp!=0) {
cin>>userinp;
if (userinp=0) userinp=0;
else if(userinp<0||userinp>9999) cout<<"Wrong input";
else if (userinp<10) { dig1++; userinp =-1;}
else if (userinp<100) {dig2++; userinp =-1;}
else if (userinp<1000) {dig3++; userinp =-1;}
else {dig4++; userinp =-1;}
}
cout<<dig1<<endl<<dig2<<endl<<dig3<<endl<<dig4;
return 0;
}
我正在尝试课本上的练习,但它卡在无限循环中,因为当我输入 0 时它会运行else if userinp<10
。为什么?如果我在 if 熄灭后删除部分。
您在 if 条件部分中放置了一个赋值 (=( 而不是相等检查 (==(。
更改此行:
if (userinp=0) userinp=0;
对此
if (userinp==0) userinp=0;
你应该很好。
祝你好运
相关文章:
- 为什么C++的文件 I/O 在读取文本文件时忽略初始空行?我怎样才能让它不这样做?
- 为什么在C++中对链表这样做?(像堆叠一样处理它们)
- C++ 如果在 if 为 true 之后运行,为什么还会这样做
- 为什么 arr[i++] 与 arr[i]++ 会这样做?
- std::字符串 's' 通过 '&s[0]' 转换为字符* - 如何/为什么这样做?
- C++子字符串为什么这样做
- C++循环、作用域和堆栈(为什么这样做?
- 为什么这样做?C中的字符指针
- C++中的字符串分配:为什么这样做
- 带有1个参数的C++2D数组索引(为什么这样做?)
- 为什么这样做?[C++;无效指针]
- 线程之间的Qt连接类型:为什么这样做
- 在派生类中同时分配多个字段-为什么这样做有效
- 为什么这样做?阵列访问不正常
- 迭代char**为什么这样做
- c++ lambda, capture, Smart Ptrs和Stack:为什么这样做?
- 在信号代码中将整数转换为函数指针-为什么这样做?
- 如果一个引用一旦被初始化为一个对象,它就不能被改变,为什么这样做呢?
- 回退可变构造函数-为什么这样做
- 访问类中的私有成员变量:为什么这样做