If statements [Calculator project]

I was developing a very simple calculator on the console and it needs a lot of if statements but non of them seem to be working! The code is the whole program with the first if statement that doen't work! PLS HELP!

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>

using namespace std;

//variables!
int choice, numberOf; //choice is the operator that the user chooses to perform and the number of is self-explanatory

float num1, num2, num3, num4, num5, num6, rating;

int main()
{
    cout << "CALCULATOR\n\nBy Dhanin Unnithan\n\n";
    cout << "Please choose an operation: \n" <<
            "1.Add\n" <<
            "2.Subtract\n" <<
            "3.Divide\n" <<
            "4.Multiply\n" <<
            "5.Modulo\n" <<
            "6.EXIT\n\n";

    cin >> choice;
        

        if (choice == 1)
            {
                cout << "How many numbers would you like to add?\n" <<
                    "2\n" <<
                    "3\n" <<
                    "4\n" <<
                    "5\n" <<
                    "6\n\n";

                cin >> numberOf;
                        if (numberOf == 2)
                        {
                            cout << "Please enter the first number: \n";
                            cin >> num1;
                            cout << "Please enter the second number: \n";
                            cin >> num2;

                            cout << num1 << " plus " << num2 << " is " << num1 + num2 << "\n\n" ;
                        }
            }
                                else if (choice == 3)
                                {
                                    cout << "Please enter the first number: \n";
                                    cin >> num1;
                                    cout << "Please enter the second number: \n";
                                    cin >> num2;
                                    cout << "Please enter the third number: \n";
                                    cin >> num3;

                                    cout << num1 << " plus " << num2 << " plus " << num3 << " is " << num1 + num2 + num3 << "\n\n6" ;
                                


    return 0;
}
Last edited on
If anyyone can help please do!! IM STUMPED!
//Now, ALL HELL BREAKS OUT!


Your description of what happens is very misleading and didn't help at all. As it is, your code doesn't compile because line 55 has a strange "az" on it.

Once I removed that, the following output was seen:

CALCULATOR

By Dhanin Unnithan

Please choose an operation: 
1.Add
2.Subtract
3.Divide
4.Multiply
5.Modulo
6.EXIT

1
How many numbers would you like to add?
2
3
4
5
6

2
Please enter the first number: 
1
Please enter the second number: 
2
1 plus 2 is 3


Works fine.
Thanks a lot! Though if you choose that you want to add 3 numbers the program just ends! Still, thanks for your help. really appreciate it!
check your code formatting for the if statements... it appears you're are missing some brackets...
You need to learn about arrays and loops.
This program can be written much more simply by using arrays and loops.
Though if you choose that you want to add 3 numbers the program just ends!


Boom. An enormous amount of extra information that you previously gave no clue whatsoever about. As Dan says, you've screwed up your brackets, but what you most need to learn is how to ask a question. Simply holding up some code and saying it doesn't work is useless and will mean people don't answer.

http://www.cplusplus.com/forum/beginner/1/
It's fine, I got it! Instead of adding the if (numberOf == 3) I added if (choice == 3)
The correct version is:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>

using namespace std;

//variables!
int choice, numberOf; //choice is the operator that the user chooses to perform and the number of is self-explanatory

float num1, num2, num3, num4, num5, num6, rating;

int main()
{
    cout << "CALCULATOR\n\nBy Dhanin Unnithan\n\n";
    cout << "Please choose an operation: \n" <<
            "1.Add\n" <<
            "2.Subtract\n" <<
            "3.Divide\n" <<
            "4.Multiply\n" <<
            "5.Modulo\n" <<
            "6.EXIT\n\n";

    cin >> choice;


        if (choice == 1)
            {
                cout << "How many numbers would you like to add?\n" <<
                    "2\n" <<
                    "3\n" <<
                    "4\n" <<
                    "5\n" <<
                    "6\n\n";

                cin >> numberOf;
                        if (numberOf == 2)
                        {
                            cout << "Please enter the first number: \n";
                            cin >> num1;
                            cout << "Please enter the second number: \n";
                            cin >> num2;

                            cout << num1 << " plus " << num2 << " is " << num1 + num2 << "\n\n" ;
                        }
            }
                        if (numberOf == 3)
                            {
                                cout << "Please enter the first number: \n";
                                cin >> num1;
                                cout << "Please enter the second number: \n";
                                cin >> num2;
                                cout << "Please enter the third number: \n";
                                cin >> num3;

                                cout << num1 << " plus " << num2 << " plus " << num3 << " is " << num1 + num2 + num3 << "\n\n6" ;
                            }


    return 0;
}
Last edited on
Please use code tags correctly. You need a
[/code]
tag after your code.
Topic archived. No new replies allowed.