If statement troubles

Hello, I've created my own calculator code; however, when I run the program the only operation that works is addition. How do I fix this?

#include "stdafx.h"

#include <iostream>

using namespace std ;

int main()
{
int x;
char y;
int z;

cout << "Imani's Calculator! \n";
cout << "Enter an operation( * , - , + , /) \n";
cin >> y;
cout << "Enter your first number: ";
cin >> x;
cout << "Enter your second number: ";
cin >> z;

if (y = '+' )
cout << x + z;
else if (y = '-')
cout << x - z;
else if (y = '/')
cout << x / z;
else if (y = '*')
cout << x * y;
else
cout << "Calculation not possible.";



char f;
cin >> f;
return 7;
}
Please use code tags; it makes your code much more readable: http://www.cplusplus.com/articles/jEywvCM9/

In Xcode, it worked fine for me. Although, you should be using if (y == '-'), etc., which depending on the compiler may be causing the problem possibly.

Also, why are you returning seven?!
I tried adding another equals sign. I tried 5 * 5 and received 210.

In a youtube video the teacher stated that it didn't matter what number was used after the return as long as it was positive.
cout << x * y; should be cout << x * z;
The program runs smoothly now. Thank you both for the help!
OP wrote:
I tried adding another equals sign. I tried 5 * 5 and received 210.

It shouldn't make a difference, as far as I know, but that is the way it should be done.

In a youtube video the teacher stated that it didn't matter what number was used after the return as long as it was positive.

Yup, but that is kind of the standard way of doing it, I guess you could say.

What gwslow said should fix it.
Last edited on
Topic archived. No new replies allowed.