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;
}
}
|