Reputation: 129
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;
}
Upvotes: 0
Views: 544
Reputation:
You need to change from this line:
cin >> a;
cin >> b;
cin >> op;
to this one:
cin >> a;
cin >> op;
cin >> b;
Upvotes: 1
Reputation:
You want your condition to be:
} while (ans != 'N' && ans != '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.
Upvotes: 4
Reputation: 613
ans will ALWAYS not equal EITHER n or N. Your OR condition is always going to pass as true. Try switching this to
while(ans == 'Y' || ans == 'y')
Upvotes: 6