Program is working fine (only with the + operator for now) but why do I need to type "X" two times to exit?
The do while should stop the first time when user type X...
cant figure out...
Because you are getting input twice ( 'operation' and 'number' )
try changing the loop to something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
while (true)
{
cout << "Enter an operation: + - * / (or enter X to exit):";
cin >> operation;
if ( operation== 'X' )
break; // exit from the loop
cout << "Enter a number: ";
cin >> number;
//...
}
You don't have to change your code. Just put an if statement, like the one Bazzy has given, straight after you get your input from the user and it should work.
Thank you for the reply guys...my code is working fine with all the operators except I dont know how to take into account of the fact that the user could select division operator / and then select 0 and dividing by zero is impossible...
this is the "working" code I wrote...but when the user input zero it would give me inf
I was thinking of changing it to something like this
1 2 3 4 5 6 7 8 9
if ((operation == '/') && (number=0))
{
cout << "Sorry cannot divide by zero";
}
else
updatedTotal /= total + number;
cout << "Current total is " << updatedTotal << endl;
}
For some reasons I still get the inf results from the division by 0...as the program would run the division anyway despite the if else statement...
Just to remind you I am in a do while loop...
I would like to stop the program when the user select divide by 0 and ask to input another number...
can you provide a suggestion
number = 0 is not a comparison. It's an assignment, which is still perfectly legal within an if(), but most of the time isn't the desired result.
= is the assignment operator. It assigns the value to the right of the operator (the right hand operand) to the variable to the left of the operator (the left hand operand).
== is the comparison operator. It will return true if left hand and right hand values are equal and return false if they are not.
First thing you should always do if your if() statement isn't working the way you intend it to, check to see if you're using = instead of ==.