do while

why doesnt this do while loop work.when i get my answer and it just skips over the "would you like to restart "?

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
#include <iostream>

using namespace std;

int main()
{
    int a;
    int b;
    int answer;
    int choice;
    char again;

    do{
    cout << "here are your options" << endl << endl;
    cout << "1.Addition" << endl;
    cout << "2.subtraction" << endl;
    cout << "3.multiplication" << endl;
    cout << "4.divison" << endl;
    cout << "5.to quit" << endl;
    cin >> choice;

    switch(choice){
        case 1:
        cout << "enter two numbers you want to use(press enter after you enter each number): ";
        cin >> a >> b;
        answer = a + b;
        cout << "the answer is: " << a << " + " << b << " = " << answer << endl;
        break;
        case 2:
        cout << "enter two numbers you want to use(press enter after you enter each number): ";
        cin >> a >> b;
        answer = a - b;
        cout << "the answer is: " << a << " - " << b << " = " << answer << endl;
        break;
        case 3:
        cout << "enter two numbers you want to use(press enter after you enter each number): ";
        cin >> a >> b;
        answer = a * b;
        cout << "the answer is: " << a << " * " << b << " = " << answer << endl;
        break;
        case 4:
        cout << "enter two numbers you want to use(press enter after you enter each number): ";
        cin >> a >> b;
        answer = a / b;
        cout << "the answer is: " << a << " / " << b << " = " << answer << endl;
        break;
        case 5:
        cout << "thanks for using this calculator";
        return 0;
        break;
    }
    cout << "would you like to restart(y or n)";
    cin >> again;
    }while(again == 'y' ||again == 'Y');

    system("pause");
    return 0;
}
Last edited on
I get the question "would you like to restart(y or n)" and if I input y I get the menu again so everything seems to work.
Hey, you aren't indenting properly. This is a severe issue and you are going to hate yourself very soon if you don't start indenting your code. Your welcome! Here is how it should look:

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
#include <iostream>

using namespace std;

int main()
{
    int a, b, answer, choice;
    char again;

    do{
        cout << "here are your options" << endl << endl;
        cout << "1.Addition" << endl;
        cout << "2.subtraction" << endl;
        cout << "3.multiplication" << endl;
        cout << "4.divison" << endl;
        cout << "5.to quit" << endl;
        cin >> choice;

        switch(choice){

            case 1:
                cout << "enter two numbers you want to use(press enter after you enter each number): ";
                cin >> a >> b;
                answer = a + b;
                cout << "the answer is: " << a << " + " << b << " = " << answer << endl;
                break;
            case 2:
                cout << "enter two numbers you want to use(press enter after you enter each number): ";
                cin >> a >> b;
                answer = a - b;
                cout << "the answer is: " << a << " - " << b << " = " << answer << endl;
                break;
            case 3:
                cout << "enter two numbers you want to use(press enter after you enter each number): ";
                cin >> a >> b;
                answer = a * b;
                cout << "the answer is: " << a << " * " << b << " = " << answer << endl;
                break;
            case 4:
                cout << "enter two numbers you want to use(press enter after you enter each number): ";
                cin >> a >> b;
                answer = a / b;
                cout << "the answer is: " << a << " / " << b << " = " << answer << endl;
                break;
            case 5:
                cout << "thanks for using this calculator";
                return 0;
                break;
            default:
                cout << "That is not an option.  Please choose an available number." << endl;
                break;
        }

        cout << "Would you like to restart?";
        cin >> again;

    } while(again == 'y' || again == 'Y');

    system("pause");
    return 0;
}


Take a look at this link: http://www.learncpp.com/cpp-tutorial/53-switch-statements/

It's a great tutorial on switch statements, and it has an example of a calculator program, albeit quite a bit different than yours.
Last edited on
Here is how it should look

One of many possible styles
Actually, not quite. There is generally only one style for indentation: indenting your code a specified number of spaces or tabs (and yes, this number can vary) every time you go into a deeper block of code.

However, what you probably meant to say is that there are many possible bracketing styles. I stuck with the one he was using.

Trust me moot, you will love yourself if you indent properly.
ok and i use code blocks and devc++....why on dev c++ it works but on code blocks it doesnt. on code blocks the error is on this line#include <iostream>



























Last edited on
Is your file .cpp? Or is it .c? Make sure it's .cpp. It needs to have the c++ file extension to work properly.
moot1, What exactly is the problem? Your loop seems to work fine according to Peter87, so what's the main problem?
i just figured it out. dev c++ you dont need #include <cstdlib> but on codeblocks you do
Ohk, so problem solved right... Mark it as solved then..... :)
how do i mark it as solved?
You'll have an option at the very top...
Should be a green check somewhere at the top. Go ahead and click it, and the topic will be marked as complete.
Topic archived. No new replies allowed.