Problem controlling user input with if statements and while loops

Hello all, been reading here for a while, first actual post, I'm a little nervous. Here goes.

I am trying to write a function, using a two dimensional array, that asks users to pick a seat on an airplane, row 1-10, seats A,B,C, or D. When the user enters their seat, the program should display an X in the correct spot. I am having a couple problems within my program.

1) When I ask the user to input their their row 1-10, I want the program to keep asking them to input the correct row if they input a value less than 1 or greater than 10 (code lines 49-55). To do this I started with an if statement, the problem was it would only loop once. For example, if the user entered 11, it would ask them to input the correct row again, but if the user entered 11 again, the program would just keep going. So I added a while loop within the if statement (lines 51-53). So now the program will keep asking the user for the correct input until it is entered, however, once the user does input the correct value, the program totally skips over the remaining else statement, and I cannot figure out why.

2) I tried replacing the if statements on lines 49 and 62 with just while loops. Which actually worked and displayed the seats with an X in the correct location, but then the program ignores lines 73-89, and just repeats lines 46/47.

3) The program runs great only if the user enters the correct values the first time.

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
// Airplane seat program
#include <iostream>
using namespace std;

void airline_seats(char seat_array[][4]);
//Asks user to pick a seat A,B,C, or D in row 1-10
//Places an X in the seat the user specifies

int main()
{
    char seat_array[10][4];
    airline_seats(seat_array);
    
    return 0;
}

void airline_seats(char seat_array[] [4])
{
    for (int i=0; i<10; i++)
    {
        seat_array [i][0] = 'A';
        seat_array [i][1] = 'B';
        seat_array [i][2] = 'C';
        seat_array [i][3] = 'D';
    }
    cout << "Do you want to pick an airline seat? Y or N?\n";
    char ans;
    cin  >> ans;
    cout << endl;
    while ((ans == 'Y') || (ans == 'y'))
    {
        
        int i,j, k;
        char l;
        
        for (i=0; i<10; i++)
        {
            cout << i+1 << " " ;
            for (j=0; j<4; j++)
            {
                cout << seat_array [i][j];
            }
            cout << endl;
        }
        
        cout << "Above are the available seats.\n";
        cout << "Please enter your desired seat row (1-10).\n";
        cin  >> k;
        if ((k < 1) || (k > 10))
        {
            cout << "Please enter a row 1-10.\n";
            cin  >> k;
            while ((k<1) || (k>10)){                    //Added while loop to keep looping until user
                cout << "Please enter a row 1-10.\n";   //enters a value 1-10
                cin  >> k;                              //However, once user enters correct value 1-10
            }                                           //the program skips over the next else statement
        }
        else
        {
            cout << "Please enter your desired seat in that row A,B,C, or D.\n";
            cin  >> l;
            if ((l == 'A') || (l == 'B') || (l == 'C') || (l == 'D'))
            {
             seat_array[(k-1)][(l-65)] = 'X'; //Sets user's input to X in the appropriate array location
            }
            else
            {
                cout << "Please enter your desired seat in that row A,B,C, or D.\n";
                cin  >> l;
            }
        }
        
        cout << endl;
        
        for (i=0; i<10; i++)
        {
            cout << i+1 << " " ;
            for (j=0; j<4; j++)
            {
                cout << seat_array [i][j];
                
            }
            cout << endl;
        }
        
        cout << endl;
        cout << "Seats marked with an X are taken.\n";
        cout << "Pick another seat? Y/N?\n";
        cin  >> ans;
    }
    
    
}
Same program but using while loops and no if statements. See point 2 above.

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
// Airplane Seat Program
#include <iostream>
using namespace std;

void airline_seats(char seat_array[][4]);
//Asks user to pick a seat A,B,C, or D in row 1-10
//Places an X in the seat the user specifies

int main()
{
    char seat_array[10][4];
    airline_seats(seat_array);
    
    return 0;
}

void airline_seats(char seat_array[] [4])
{
    for (int i=0; i<10; i++)
    {
        seat_array [i][0] = 'A';
        seat_array [i][1] = 'B';
        seat_array [i][2] = 'C';
        seat_array [i][3] = 'D';
    }
    cout << "Do you want to pick an airline seat? Y or N?\n";
    char ans;
    cin  >> ans;
    cout << endl;
    while ((ans == 'Y') || (ans == 'y'))
    {
        
        int i,j, k;
        char l;
        
        for (i=0; i<10; i++)
        {
            cout << i+1 << " " ;
            for (j=0; j<4; j++)
            {
                cout << seat_array [i][j];
            }
            cout << endl;
        }
        
        cout << "Above are the available seats.\n";
        cout << "Please enter your desired seat row (1-10).\n";
        cin  >> k;
        
        while ((k<1) || (k>10))
            {                    
                cout << "Please enter a row 1-10.\n";   
                cin  >> k;                              
            }                                          
        
       
            cout << "Please enter your desired seat in that row A,B,C, or D.\n";
            cin  >> l;
            while ((l == 'A') || (l == 'B') || (l == 'C') || (l == 'D'))
            {
             seat_array[(k-1)][(l-65)] = 'X'; //Sets user's input to X in the appropriate array location
            }
            while ((l < 'A') || (l > 'D'))
            {
                cout << "Please enter your desired seat in that row A,B,C, or D.\n";
                cin  >> l;
            }
            seat_array[(k-1)][(l-65)] = 'X';
        }
        
        cout << endl;
        
        for (int i=0; i<10; i++)
        {
            cout << i+1 << " " ;
            for (int j=0; j<4; j++)
            {
                cout << seat_array [i][j];
                
            }
            cout << endl;
        }
        
        cout << endl;
        cout << "Seats marked with an X are taken.\n";
        cout << "Pick another seat? Y/N?\n";
        cin  >> ans;
    
    
    
}
Couple things starting out:

1. You are using variable names such as k and l to represent user input. Make these names something meaningful like rowNum and seatNum, respectively. Will help with debugging and readability, especially with longer programs.

2. If you get rid of the if statement on line 49 and replace it with a while loop, it will solve the issue on skipping the else statement. The reason it is skipping is that, if it goes into the if statement, it always skips the else statement. It is an either/or situation. You can use else if to solve this.

3. The while loop on line 59 (on the 2nd program) will keep looping even if the user enters a valid input. change it to an if statement so that it only enters once. You also repeat seat_array[(k-1)][(l-65)] = 'X'; in line 68, so it assigns it twice.

3. The reason lines 73-89 never runs is because your while ((ans == 'Y') || (ans == 'y')) loop ends on line 69. check your indentations and your end brackets again.
pilotnate, thank you so much for taking the time to look over my code. Your tips/suggestions were really helpful. I replaced the if statement on 49 with a while loop, then switched the order of the while loops on program 2 lines 49-69. Now my program runs great! Here is the modified code.

However now I want to enhance my program, so that if the user enters a seat that already has an X in the spot, I inform them that that seat is taken and ask them to try again. However this is tricky because once the user selects a seat, the value in the array is set to X. So any kind of loop or statement to check for an X will always tell the user the seat is taken because their input is automatically initialized to X. Any tips for this enhancement?

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
// Problem 11
#include <iostream>
using namespace std;

void airline_seats(char seat_array[][4]);
//Asks user to pick a seat A,B,C, or D in row 1-10
//Places an X in the seat the user specifies

int main()
{
    char seat_array[10][4];
    airline_seats(seat_array);
    
    return 0;
}

void airline_seats(char seat_array[] [4])
{
    for (int i=0; i<10; i++)
    {
        seat_array [i][0] = 'A';
        seat_array [i][1] = 'B';
        seat_array [i][2] = 'C';
        seat_array [i][3] = 'D';
    }
    cout << "Do you want to pick an airline seat? Y or N?\n";
    char ans;
    cin  >> ans;
    cout << endl;
    while ((ans == 'Y') || (ans == 'y'))
    {
        
        int i,j, seat_row;
        char seat_letter;
        
        for (i=0; i<10; i++)
        {
            cout << i+1 << " " ;
            for (j=0; j<4; j++)
            {
                cout << seat_array [i][j];
            }
            cout << endl;
        }
        
        cout << "Above are the available seats.\n";
        cout << "Please enter your desired seat row (1-10).\n";
        cin  >> seat_row;
        
        while ((seat_row < 1) || (seat_row > 10))
            {
                cout << "Please enter a row 1-10.\n";
                cin  >> seat_row;
            }
        
        
        
        cout << "Please enter your desired seat in that row A,B,C, or D.\n";
        cin  >> seat_letter;
        while ((seat_letter < 'A') || (seat_letter > 'D'))
               {
                   cout << "Please enter your desired seat in that row A,B,C, or D.\n";
                   cout << "Make sure to capitalize your seat letter.\n";
                   cin  >> seat_letter;
               }
        
            if ((seat_letter == 'A') || (seat_letter == 'B') || (seat_letter == 'C') || (seat_letter == 'D'))
            {
             seat_array[(seat_row-1)][(seat_letter-65)] = 'X'; //Sets user's input to X in the appropriate array location
            }
               
        
        cout << endl;
        
        for (i=0; i<10; i++)
        {
            cout << i+1 << " " ;
            for (j=0; j<4; j++)
            {
                cout << seat_array [i][j];
                
            }
            cout << endl;
        }
        
        cout << endl;
        cout << "Seats marked with an X are taken.\n";
        cout << "Pick another seat? Y/N?\n";
        cin  >> ans;
        }
    }
Last edited on
I'm thinking the check could go in line 72, but this is problematic because I just initialized seat_array[(seat_row-1)][(seat_letter-65)] = 'X';

so the tactics I've tried always tell the user the seat is taken, how can I get around this?
Topic archived. No new replies allowed.