# Can't figure out what is wrong with this

• 02-20-2006
Surfndog
Can't figure out what is wrong with this
I am trying to design an algorithm for a simple calculator function. But everytime i enter in a symbol to have it perform an operation it always adds. Also, the while loop never terminates even when i say no.

#include <iostream>
using namespace std;
Code:

```int main () {         // Variable Declarations         int num1, num2, num3, num4;         char letter, symbol;         // Prompt         cout << "This Program will add, subtract, multiply, and divide"                 << "the two numbers you enter." << endl;     cout << endl;         while (letter = 'y')         {                 cout << "Please enter two numbers: ";                 cin >> num1 >> num2;                 cout << endl;                 cout << "Please input the operation you would like to use: ";                 cin >> symbol;                 cout << endl;                 if (symbol = '+')                 {                         num3 = num1 + num2;                     cout << "The sum is: " << num3 << endl;                 }         else if (symbol = '-')                 {                      num3 = num1 - num2;                     cout << "The difference is: " << num3 << endl;                 }                 else if (symbol = '*')                 {                      num3 = num1 * num2;                     cout << "The product is: " << num3 << endl;                   }                 else if (symbol = '/')                   {                      num3 = num1 / num2;                     num4 = num1 % num2;                         cout << "The quiotient is: " << num3 << "The remainder is: " << num4 << endl;                   }                 else                         cout << "Invalid symbol." << endl;                         cout << "Would you like to run the program again [y/n] ";                 cin >> letter;                 cout << endl;         }         cout << "End of Program" << endl;         return 0; }```
• 02-20-2006
Tonto
'==' is the comparison operator.
'=' is the assignment operator.

Common mistake.
• 02-20-2006
yahn
Also, where are you assigning letter to 'y'? (Besides in the while loop, where you obviously don't want to. That is the reason your while loop will not terminate.)
• 02-21-2006
Richie T
with regards the previous post and the code you supplied, i am
assuming that you want to loop over while the user enters 'y'.
you have:

Code:

`while (letter = 'y')`
which as already pointed out, should be changed to:

Code:

`while (letter == 'y')`
thats fine, but then when you start your program, what value
is in the variable y? when you declare a variable, it already has
some junk value in it (left over from something else). you will
need to initialise the variable letter as follows:

Code:

`char letter = 'y', symbol;`
otherwise, even if you make the changes suggested, the program
still wont work. just trying to explain something that is easily
overlooked by beginners