Jul 24, 2014 at 10:06pm UTC
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;
}
Jul 24, 2014 at 10:27pm UTC
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.
Jul 24, 2014 at 10:29pm UTC
cout << x * y;
should be cout << x * z;
Jul 24, 2014 at 10:35pm UTC
The program runs smoothly now. Thank you both for the help!
Jul 24, 2014 at 10:36pm UTC
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 Jul 24, 2014 at 10:36pm UTC