Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <iostream>
using namespace std;

int
main ()
{
  int x, y, z;
  int sum;
  cout << "1 = add, 2 = subtract, 3 = multiply 4 = divide: ";
  cin >> z;
  cout << "Type a number: ";
  cin >> x;
  cout << "Type a second number: ";
  cin >> y;
  if (z = 1)
    sum = x + y;
  if (z = 2)
    sum = x - y;
  if (z = 3)
    sum = x * y;
  if (z = 4)
    sum = x / y;
  cout << "answer is: " << sum;
  return 0;
}




It keeps just trying to divide everything instead of anything else and I don't know what the problem is.

What I have tried:

It started when I got a suggestion from a friend to change sum to answer (it was originally just for adding bc I didn't know how to use everything else yet). I did it and it stoped working so I put it back to normal but it still wasn't working.
Posted
Updated 10-Mar-23 10:04am
v2
Comments
jeron1 10-Mar-23 15:47pm    
The comparison operator used in the if statement should be == not =. The single equal sign is an assignment operator, which is setting the z variable to whatever is after the equals sign. Also, the result of the division is going to be an integer (no fraction), perhaps not what you wanted.
OriginalGriff 11-Mar-23 1:43am    
Answer updated.

Ton compare z with 1, need to replace
C++
if (z = 1)

with
C++
if (z == 1)
 
Share this answer
 
To add to what Patrice has said, "=" is an assignment operator: The value on the right hand side is "put into" the variable on the left. "==" is a comparison operator which returns either 0 or 1 depending on the two values.
If they re the same, it returns 0. If not, it returns 1.

But C++ is based on the much older language C where 0 means "false" and any non-zero value means "true" - and an assignment operator returns the value that it set, so you can "chain" them:
C++
a = b = c = 666;
assigns the value 666 to c, then returns 666 which is then assigned to b, which returns 666 to be assigned to a.
So when you write
if (z = 1)
the value of z is overwritten with 1 and 1 is returned to be used as the comparison - and since it's non-zero it is always true.
And the same thing happens with the next comparison!

I'd also suggest that you look at either
if (z == 1)
   ...
else if (z == 2)
   ...
else if (z == 3)
   ...
else if (z == 4)
   ...
else
   ... tell him you don't understand ...
or better, use a switch block.
 
Share this answer
 
v2
Comments
FreedMalloc 10-Mar-23 18:17pm    
Above you stated: where 0 means "true" and any non-zero value means "false". Don't you have that backwards? 0 == false, non-zero == true.
OriginalGriff 11-Mar-23 1:41am    
:doh:
... In my defence, it was getting late, and I had been up since 04:00 ...

No excuse for sloppiness though ... :O
OriginalGriff 11-Mar-23 1:42am    
fixed. Thanks for that!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900