I'm bad help me why doesn't this work
Code:
#include <iostream>
using namespace std;
int main() {
int x;
int a, b;
cout << "welcome to the calculator" << endl;
cout << "enter 1 for division, 2 for multiplication, 3 for addition and 4 for subtraction" << endl;
cin >> x;
int sum;
if (x == 1) {
sum = a / b;
}
if (x == 2) {
sum = a * b;
}
if (x == 3) {
sum = a + b;
}
if (x == 4) {
sum = a - b;
}
cout << "enter a number" << endl;
cin >> a;
cout << "one more please" << endl;
cin >> b;
cout << "and your answer is!" << endl;
cout << sum;
return 0;
}
It just says - 37 or 0. I want it to be able to calculate stuff
Re: I'm bad help me why doesn't this work
When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'.
You first define the variables a and b without specifying their values. You then use them in calculations to produce a value for sum. Then after the calculations you ask the user to input the values for a and b. However the entered values are not used as the input comes after they are used. You need to have the cout/cin statements referring to a and b before these variables are used in the calculation for sum.