+ 1

So I'm trying to create a calculator with c++ to practice the conditionals but It doesn't work any help?

this is the program so far (I'll skip include iostream and etc) int answer; int num1; int num2; cout << "What kind of calclulation do you want to do? " ; cin >> answer ; if (answer == addition) { cout << "select your first number " ; cin >> num1 ; cout << "select your second number " ; cin >> num2; cout << "This equals to " << num1 + num2; And then I did the same but instead of audition I did subtraction...you get the idea. Now I get an error that addition is unidentified I can guess why this is wrong but how can I make this work. Also I am a complete begginer so sorry if the answer is something stupid. I also tried puting addition in "" but then I got an error saying that the == are incompatible

29th Jul 2018, 7:22 PM
Elias Kamakas
Elias Kamakas - avatar
2 odpowiedzi
+ 7
You are reading an integer so you need to test it against one. For example, prompt with this: What kind of calclulation do you want to do? 1=addition, 2=subtraction then, test this: if (answer == 1) {...} else if (answer == 2) {...}
29th Jul 2018, 7:55 PM
John Wells
John Wells - avatar
+ 2
https://code.sololearn.com/c8wUuxNq8YhZ/?ref=app That's an example of a simple calculator. I didn't use any if statements here, though. Your problem is that you have declared 'answer' variable as an integer. The solution might be: cout << "Type 1 for addition, 2 for subtraction, 3 for..."; cin >> answer; if (answer == 1) { ... } else if (answer == 2) { ... } else...
29th Jul 2018, 7:52 PM
Steppenwolf
Steppenwolf - avatar