Need Help calculating

So I did it this far, I'm not really sure what to do after this.
How do I do the operation of two integers that gives you the results?

I'm supposed to write a program that:

Asks the user for an integer
Asks the user for one of '+', '-', '*', or '/' (as a character)
Asks the user for another integer
Performs the given operation on the two integers, and prints out the result of

Please enter an integer: 4
Please enter an operation (+, - , *, /): *
Please enter another integer: 5

4 * 5 = 20




[code]
#include <iostream>
using namespace std;

int main() {
int x;
int c;
int d;
char e;

cout << "Please enter an integer: ";
cin >> x;

cout << "Please enter one of operation (+, - , *, /): *";
cin >> e;

cout << "Please enter a second integer";
cin >> c;

if (e == '+')
cout << x << e << c << " = " << x + c;
else if (e == '-')
cout << x << e << c << " =" << x +d;

system("pause");
return 0;
[code]
Last edited on
You forgot to ask a question.
What should I do after this part?
if (e == '+')
cout << x << e << c << " = " << x + c;
else if (e == '-')
cout << x << e << c << " =" << x +d;
How did you know what to do for addition and subtraction but you don't know what to do for multiplication and division?
I wasn't sure if I did that right either
I don't know why you have d at all. If you fix that it should be correct. Just test the program if you're not sure.
Is this better?

#include <iostream>
using namespace std;

int main() {
int x;
int a;
int b;
int c;
int d;
char e;

cout << "Please enter an integer: ";
cin >> x;

cout << "Please enter one of operation (+, - , *, /): *";
cin >> e;

cout << "Please enter a second integer";
cin >> c;

if (e == '+')
cout << x << e << a << " = " << x + a;
else if (e == '-')
cout << x << e << b << " = " << x - b;
else if (e == '*')
cout << x << e << c << " = " << x * c;
else if (e == '/')
cout << x << e << d << " = " << x / d;
You appear to be under the impression that the variable used for the second integer changes, it does not. Only declare x, e and c and try something like:
1
2
3
4
5
6
7
8
if (e == '+')
cout << x << e << c << " = " << x + c; 
else if (e == '-')
cout << x << e << c << " = " << x - c; 
else if (e == '*')
cout << x << e << c << " = " << x * c; 
else if (e == '/') 
cout << x << e << c << " = " << x / c; 


You may also want to give the variables more appropriate names, it'll make it easier in the long run.
Last edited on
Topic archived. No new replies allowed.