You could solve this in your sleep...

I just recently (yesterday infact) started to learn c++ and am finding it pretty entertaining; However I have had no programming experience before this so my progress has likely been quite slow by most standards.

After working at some tutorials I started writing a sort of calculator program. The problem is there are a couple of things I'm really stumped with that repeated googling can't seem to solve. Could anyone help?

Here's the code first of all:

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//stuff

#include <iostream>
#include <cmath>
using namespace std;

int main ()

{


/* This is the initial selection stuff
that lets people choose the kind of
mathmatics they want to do... hopefully*/


int selection;
double x;
double y;
float add, subtract, mult, div;


{
    cout<<"Please make a selection.\n";
    cout<< "1. To add\n";
    cout<< "2. To subtract\n";
    cout<< "3. To multiply\n";
    cout<< "4. To divide\n";
    cout<< "0. to exit\n";

    cin>> selection;
    cin.get();
}


        ///////////////////////    The add section

    if (selection == 1);
    {

        cout<< "Please enter the two numbers you want to add followed by the enter key." << endl;
        cin >> x >> y;
        add = x + y;
        cout<< "Your answer is " << add << ".\n";
        return 0;
    }



        //////////////////////  The subtract bit.
    if (selection == 2)
    {

        cout<< "Please enter a number followed by the number you wish to subtract from it." << endl;
        cin >> x >> y;
        subtract = x - y;
        cout<< "Your answer is " << subtract << ".\n";

    }


        //////////////////////  Multiplication
    if (selection == 3)
    {

        cout<< "Please enter the two numbers you wish to multiply." << endl;
        cin >> x >> y;
        mult = x * y;
        cout<< "Your answer is " << mult << ".\n";


    }

        /////////////////////   Divison
    if (selection == 4)
    {

        cout<< "Please enter the two numbers divide" << endl;
        cin >> x >> y;
        div = x / y;
        cout<< "Your answer is " << div << ".\n";

    }

        //////////////////// The exit selection
    if (selection == 0)
    {

        cout<< "\nThankyou! - Matt.\n";
        return 0;

    }


}



It's almost fully functional. I somehow broke the exit option (I'm sure it worked at one point...), I'm still trying to work that one out, but it isn't my main problem.

What I REALLY want to know is how to make each arithmetic section loop back to the main menu to allow another selection! I've spent quite a while trying the various loop statements but it's seems I don't have a clue. Something I suspected all along...

I'm pretty sure my codes fairly ridiculous aswell but I wrote it only way I know how at the moment. Hope no one gets a headache!

Any help at all on my loop problem, my exit code or just the way I've written the thing would be greatly appreciated!

Thanks.
Ok, here it goes:
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//stuff

#include <iostream>
#include <cmath>
using namespace std;

int main ()

{


/* This is the initial selection stuff
that lets people choose the kind of
mathmatics they want to do... hopefully*/


int selection;
double x;
double y;
float add, subtract, mult, div;


do {
    cout<<"Please make a selection.\n";
    cout<< "1. To add\n";
    cout<< "2. To subtract\n";
    cout<< "3. To multiply\n";
    cout<< "4. To divide\n";
    cout<< "0. to exit\n";

    cin>> selection;
    cin.get();


        ///////////////////////    The add section

    if (selection == 1);
    {

        cout<< "Please enter the two numbers you want to add followed by the enter key." << endl;
        cin >> x >> y;
        add = x + y;
        cout<< "Your answer is " << add << ".\n";
    }



        //////////////////////  The subtract bit.
    if (selection == 2)
    {

        cout<< "Please enter a number followed by the number you wish to subtract from it." << endl;
        cin >> x >> y;
        subtract = x - y;
        cout<< "Your answer is " << subtract << ".\n";

    }


        //////////////////////  Multiplication
    if (selection == 3)
    {

        cout<< "Please enter the two numbers you wish to multiply." << endl;
        cin >> x >> y;
        mult = x * y;
        cout<< "Your answer is " << mult << ".\n";


    }

        /////////////////////   Divison
    if (selection == 4)
    {

        cout<< "Please enter the two numbers divide" << endl;
        cin >> x >> y;
        div = x / y;
        cout<< "Your answer is " << div << ".\n";

    }

        //////////////////// The exit selection
    if (selection == 0)
    {

        cout<< "\nThankyou! - Matt.\n";

    }

} while(selection != 0);

return 0;

}
you can use the switch statement and should tell when the user inputs a wrong number
Last edited on
coder777 beat me to it!

As he mentioned, you should look into the switch statement. It looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
switch (expression)
{
  case 1:
    //statements here...
    //no curly brackets needed for multiple statements
    break;
  case 2:
    //statements here...
    //no curly brackets needed for multiple statements
    break;
  default:
    break;
}


You can find a tutorial on the switch-statement on this site. The switch-statement should reduce your code length from almost a hundred lines to about 60.

He also beat me to the do while-loop.
Thanks to the both of you

I think i'm a lot closer to understanding the do...while loop. Before seeing this example I was under the impression that each section needed it's own while statement. Now I see it working it makes 100 times more sense. :).

Big thanks for the switch advice aswell. I'm making that my next read. I think I can keep altering and modifying (breaking?) this code as I work through the tutorials on this site. I was using the lessons over at http://www.cprogramming.com/ before but I found them much harder to follow than the ones here. Whoever wrote them should be proud of themselves.

Thanks again!
Topic archived. No new replies allowed.