std :: cin的char造成无限循环 [英] std::cin of char causing infinite loop
本文介绍了std :: cin的char造成无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用cout语句之后,在切换之前,确定我有一个问题,第二个数字和字符操作,由于某种原因,我陷入无限循环!
我有一种感觉,我在语法上犯了一个小错误,但我不能弄清楚它是什么。
I've used the cout statement after the cin's before the switch to determine that I'm having a problem with the second number and the character op and for some reason I get into an infinite loop!!! I have a feeling that I'm making a small mistake in the syntax but I cant figure out what is it.
#include<iostream>
using namespace std;
int main()
{
float a, b;
char op, ans;
do {
cout << "Enter first number, operator, second number: ";
cin >> a;
cin >> b;
cin >> op;
cout << "first number is: " << a << "second number is: " << b << "operator is: " << op;
switch (op) {
case '/':
cout << "answer = " << a / b << endl;
break;
case '*':
cout << "answer = " << a * b << endl;
break;
case '+':
cout << "answer = " << a + b << endl;
break;
case '-':
cout << "answer = " << a - b << endl;
break;
}
cout << "again? Y/N";
cin >> ans;
} while (ans != 'N' || ans != 'n');
cout << endl;
system("pause");
return 0;
}
推荐答案
:
} while (ans != 'N' && ans != 'n');
不能同时为N和n。如果是这样的话,世界可能会消失在厄运的漩涡中。
It can't not be 'N' and 'n' simultaneously. If that were the case, the world would probably disappear into a vortex of doom.
这篇关于std :: cin的char造成无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文