help me please?

help me with my codes. how do i end the loop when i enter a value and by not breaking the loop? because when i enter a value in the seat[a] it keeps on repeating. it should return in the main loop which is the for(b=0; b<=10; b++). help me please.

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
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
    int seat[10];
    int n, a, b, c;
    int loop;
    int type;
    string choice;
           
    for(b = 0; b<=10; b++)
    {
    cout << "Please type 1 for Smoking" << endl;
    cout << "Please type 2 for Non-smoking" << endl;
    cin >> type;
    if(type != 1 && type != 2)
    {
    cout << "Please type the right number." << endl;
    return main();
    }
    if(type == 1)
    {
            for(a = 0; a<=10; a++)
            {
            if(seat[a] == seat[5])
                 {
                            cout << "Sorry all the seats in Smoking area are occupied." << endl;
                            cout << "Would you like to go in Non-smoking area instead?" << endl;
                            cout << "If yes type Y and if no type N: ";
                            cin >> choice; 
                            
                }
            cout << "*--- Welcome to the Smoking area ---*" << endl;
            do
            {
                 cout << "Please choose a seat from 1-5: " << endl;
                 cin >> seat[a];
                 for(n = 0; n<a; n++)
                 if(seat[a] == seat[n])
                 {
                            cout << "Sorry, the seat is already taken." << endl << endl;
                            break;
                 }
                 else if(seat[a] > 5)
                 {
                 cout << "Sorry you put an invalid seat number." << endl << endl;
                 break;
                 }
            }
            while(n != a);
            if(seat[a] <= 5)
            {
                    cout << "Your seat number is: " << seat[n] << endl;
                    cout << "------------------------------" << endl;
            }
            }
            }
    if(type == 2)
    {
            for(c = 0; c<=10; c++)
            {
                  if(seat[c] == seat[5])
                 {
                            cout << "Sorry all the seats in Non-smoking area area occupied." << endl;
                            cout << "Would you like to go in Smoking area instead?" << endl;
                            cout << "If yes type Y and if no type N: ";
                            cin >> choice; 
                }
            cout << "Welcome to the Non-smoking area" << endl;
            do
            {
                 cout << "Please choose a seat from 6-10: " << endl;
                 cin >> seat[c];
                 for(n = 0; n<c; n++)
                 if(seat[c] == seat[n])
                 {
                  cout << "Sorry, the seat is already taken." << endl;
                  break;
                 }
                 else if(seat[c] < 5)
                 {
                 cout << "Sorry you put an invalid seat number." << endl;
                 break;
                 }
            }
            while(n != c);
            if(seat[c] >= 10 || seat[c] <= 10)
            {
                       cout << "Your seat number is: " << seat[n] << endl;
                       cout << "------------------------------" << endl;
            }
            }
            }
    if(b == 10)
    {
        cout << "Sorry all the seats are already taken." << endl;
    }
}
    system("PAUSE");
    return 0;
}
 

Last edited on
First, to make a loop repeat there are many options and none of those options include calling 'main'.

Easiest way to make the program continuously loop without using up extra seats will be to simply refuse giving up the seat by subtracting one from b. So instead of doing:

1
2
3
4
5
    if(type != 1 && type != 2)
    {
        cout << "Please type the right number." << endl;
        return main();
    }


You do:

1
2
3
4
5
6
if(type != 1 && type != 2)
    {
        cout << "Please type the right number." << endl;
        b--;
        continue;
    }


Also to avoid extra work, use switch statements http://www.cplusplus.com/doc/tutorial/control statements or elseif
Last edited on
Topic archived. No new replies allowed.