Add and Subtract the perform operation

Using C++ I am trying to have the user select add or subtract and then perform the operation. Please let me me know where i went wrong. Thank You!

#include<iostream>
using namespace std;
int main ()
{
int a;
int b;
int answer;
int choice;

cout << " 1 - Addition ";
cout << " 2 - Subtraction ";
cin >> answer;
{
if(choice==1)
{
cout << "Great, please enter a number...\n";
cin >> a;
cout << "Thanks, now enter a number to add to that...\n";
cin >> b;
(answer = a + b);
cout << "All done, the answer is" << answer << "\n\n";
}
if(choice==2)
{
cout << "Great, please enter a number...\n";
cin >> a;
cout << "Thanks, now enter a number to subtract from that...\n";
cin >> b;
(answer = a - b);
cout << "All done, the answer is" << answer << "\n\n";
}

return 0;
}
1
2
3
cin >> answer;
{
    if(choice==1)


You store the user's input in the variable "answer" but then you check the value of the variable "choice".
I did change the variable but it is still not working!
You were also missing a curly bracket. Now it is ok:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
using namespace std;
int main ()
{
    int a;
    int b;
    int answer;
    int choice;

    cout << " 1 - Addition ";
    cout << " 2 - Subtraction ";
    cin >> choice;
    {
        if(choice==1) {
            cout << "Great, please enter a number...\n";
            cin >> a;
            cout << "Thanks, now enter a number to add to that...\n";
            cin >> b;
            (answer = a + b);
            cout << "All done, the answer is" << answer << "\n\n";
        }
        if(choice==2) {
            cout << "Great, please enter a number...\n";
            cin >> a;
            cout << "Thanks, now enter a number to subtract from that...\n";
            cin >> b;
            (answer = a - b);
            cout << "All done, the answer is" << answer << "\n\n";
        }

        return 0;
    }
}
Thank you, that work well, my red light (error) on Code Blocks disappeared, but my blank screen that should pop up asking me to select a choice is not coming up.

do you think i should put cin.get() or system("PAUSE") above the return 0;
No, in Code::Blocks I don't think you need any trick to keep the console open.
Anyway that code is working for me, so it should work for you as well!
Thanks my friend, i used the same code, and the console will not open, what should i do?
My teacher just email me and said that my Main() should just have fewer then 5 lines in it.

What does that mean?
Are you supposed to make separate functions and just call them from main?

Edit: If not - there is some duplication in your cases. You might be able to consolidate that.
Last edited on
I had to just confirm what i said, it opens in Microsoft Visual Studio, but it the console will not open in Codeblocks. So you are right it does work, i just had to let you know you were right.
Yes, I am suppose to make separate functions and just call them main!
Thank you minomic (151) you rock!!
Topic archived. No new replies allowed.