when doing std::cin >> x; std::cin think it gets a float as input, since the character is no float std::cin says it can't read the data and sets the failbit and the badbit.
to be more concrete: for number types std::cin only accepts characters between '0' and '9' and for float it also accepts '.'
Since '=' is none of those characters std::cin sets its badbit and failbit and just does nothing when asked for data.
if failbit or badbit are set then std::cin will be skipped.
You can check those.
If the failbit or the badbit is set it means that something failed (for example there is an invalid input)
You have to clear the badbit and the failbit before you can read from the buffer again.
// Example program
#include <iostream>
#include <string>
int main()
{
float x = 0;
char input = '\0';
while(input != '=')
{
if(std::cin.good()) { // if the state is good
std::cout << "Enter Number"; // ask for a number
std::cin >> x; // read a float
}
else { // if either badbit or failbit are set
std::cin.clear(); // clear badbit and failbit
std::cin >> input; // read the character that has been written
}
}
}
It seems like this simple question was quite complicated.
#include <iostream>
usingnamespace std;
int main()
{
int x , y , z;
cout<<"Please enter value of x"<<endl;
cin>>x;
cout<<"Please enter value of y"<<endl;
cin>>y;
char op;
cout<<"Enter arithematic operation"<<endl;
cin>>op;
switch(op)
{
case'+' :
z = x+y; cout<<"The value of z is : "<<z<<endl;break;
case'-' :
z=x-y;
case'*' :
z= x * y ; cout<<"The value of z is : "<<z<<endl;break;
case'/' :
z = x/y; cout<<"The value of z is : "<<z<<endl;break;
default :
cout<<"Invalid operator " ;break;
}
cin.get();
cin.ignore();
return 0;
}