Loopint with a switch statement

not long started c++ and i am looking at getting the user to enter the task they want to do, then the program asks is they have another task, and loops until the user replies no or 2.

the problem i am having is getting the program to loop back to the cin>>task part

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

int database = 0;
int spreadsheet= 0;
int word = 0;
int ppt = 0;
int img = 0;
int cont;



using namespace std;
int main()
{
    int task;
    cout<<"what task would you like to perform?"<<endl;
    cout<<" 1 = Open a database"<<endl;
    cout<<" 2 = Open a Spreadsheet"<<endl;
    cout<<" 3 = Open a Word Document"<<endl;
    cout<<" 4 = Open a Presentation"<<endl;
    cout<<" 5 = Open an Image"<<endl;
cout<<"continue? 1 = yes 2 = exit"<<endl;
cin>>cont;
cout<<"please eneter which task you want to perform"<<endl;
cin>>task;
cout<<"---------------------------------------------------------------"<<endl;

   if (cont == 1)

       switch(task)

    {
        case 1:
        cout<<"you have opened a database, enjoy"<<endl;
        database ++;
        cout<<"you have opened "<<database<<" databases today"<<endl;
        cout<<"continue? 1 = yes 2 = exit"<<endl;
        cin>>cont;


        //CASE 2
        case 2:
        cout<<"you have opened a spreadsheet, enjoy"<<endl;
        spreadsheet ++;
        cout<<"you have opened "<<spreadsheet<<" spreadsheets today"<<endl;
        cout<<"continue? 1 = yes 2 = exit"<<endl;
        cin>>cont;
        break;

        case 3:
        cout<<"you have opened a Word Document"<<endl;
        word ++;
        cout<<"you have opened "<<word<< " word documents today"<<endl;
        cout<<"continue? 1 = yes 2 = exit"<<endl;
        cin>>cont;
        break;

        case 4:
        cout<<"you have opened a Powerpoint Presentation"<<endl;
        ppt++;
        cout<<"you have opened "<<ppt<<" presentations today"<<endl;
        cout<<"continue? 1 = yes 2 = exit"<<endl;
        cin>>cont;
        break;

        case 5:
        cout<<"you have opened an image"<<endl;
        img++;
        cout<<"you have opened "<<img<<" images today"<<endl;
        cout<<"continue? 1 = yes 2 = exit"<<endl;
        cin>>cont;

    }}


Try a while loop that goes on as long as your cont variable isn't 2 (your exit code).

1
2
3
4
while(cont != 2)
{
    //Get task and do switch
}


Then you move your code for getting the task into the loop, also put the switch in the loop, so you keep on asking for tasks and preforming that task for as long as the user didn't enter 2.

Your complete code would then be something like this:

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

int database = 0;
int spreadsheet = 0;
int word = 0;
int ppt = 0;
int img = 0;
int cont;



using namespace std;
int main()
{
    int task;
    cout << "what task would you like to perform?" << endl;
    cout << " 1 = Open a database" << endl;
    cout << " 2 = Open a Spreadsheet" << endl;
    cout << " 3 = Open a Word Document" << endl;
    cout << " 4 = Open a Presentation" << endl;
    cout << " 5 = Open an Image" << endl;
    cout << "continue? 1 = yes 2 = exit" << endl;
    cin >> cont;

    while(cont != 2)
    {
        cout << "please eneter which task you want to perform" << endl;
        cin >> task;
        cout << "---------------------------------------------------------------" << endl;
        switch(task)
        {
            case 1:
                cout << "you have opened a database, enjoy" << endl;
                database ++;
                cout << "you have opened " << database << " databases today" << endl;
                cout << "continue? 1 = yes 2 = exit" << endl;
                cin >> cont;
                break;
            //CASE 2
            case 2:
                cout << "you have opened a spreadsheet, enjoy" << endl;
                spreadsheet ++;
                cout << "you have opened " << spreadsheet << " spreadsheets today" << endl;
                cout << "continue? 1 = yes 2 = exit" << endl;
                cin >> cont;
                break;

            case 3:
                cout << "you have opened a Word Document" << endl;
                word ++;
                cout << "you have opened " << word << " word documents today" << endl;
                cout << "continue? 1 = yes 2 = exit" << endl;
                cin >> cont;
                break;

            case 4:
                cout << "you have opened a Powerpoint Presentation" << endl;
                ppt++;
                cout << "you have opened " << ppt << " presentations today" << endl;
                cout << "continue? 1 = yes 2 = exit" << endl;
                cin >> cont;
                break;

            case 5:
                cout << "you have opened an image" << endl;
                img++;
                cout << "you have opened " << img << " images today" << endl;
                cout << "continue? 1 = yes 2 = exit" << endl;
                cin >> cont;
                break;
        }
    }
    return 0;
}

Topic archived. No new replies allowed.