Im working on a program that will let you reserve plane seats and most of its working fine, except it won't mention if there's an invalid input or if a seat's already been reserved under certain conditions. It just says that the seat is taken every time. What am I doing wrong?
Here's the code:
#include <cstdlib>
#include <iostream>
usingnamespace std;
constint num_row = 7;
constint num_column = 5;
int main()
{
int index1, index2;
char arr[num_row][num_column];
char letter = 'A';
char num = '1';
char in1, in2, again;
for (index1 =0; index1 < 7; index1++) // this loop assign A B C D to all 7 rows
{
for (index2 = 1; index2 <5; index2++)
{
arr[index1][index2] = letter;
letter++;
}
letter = 'A';
}
for (index1= 0; index1< 7; index1++) //This loop assign the number 1 to 7 to the 7 rows
{
arr[index1][0] = num;
num++;
}
for (index1 = 0; index1 < 7; index1++) // this loop shows the plane seats
{
for (index2 = 0; index2 < 5; index2++)
{
cout << arr[index1][index2] << " ";
}
cout << endl;
}
cout << endl;
do // this loop repeat the process
{
cout << "Please enter a seat to return, or N to exit.\n";
cin >> (in1); // getting first character
cin >> (in2); //getting second character
in2 = toupper(in2);
for (index1 = 0; index1 < 7; index1++) //this loop mark X to a seat
{
if (in1 == arr[index1][0])
{for (index2 = 1; index2 < 5; index2++)
{if (in2 == arr[index1][index2])
{arr[index1][index2] = 'X';}
elseif (in2 != arr[index1][0])
{
cout << "Seat is taken. Please try again.\n";
}
else
{
cout << "Invalid input. Please pick another seat.\n";
}
}
}
}
for (index1 = 0; index1 < 7; index1++) //This loop shows whats happen after the seat is marked
{
{
for (index2 = 0; index2 < 5; index2++)
{
cout << arr[index1][index2] << " ";
}
cout << endl;
}
}
cout << "Please enter Y, then enter another seat to return, or N to exit.\n";
cin >> again;
}while (toupper(again) == 'Y');
}
#include <cstdlib>
#include <iostream>
usingnamespace std;
constint num_row = 7;
constint num_column = 5;
int main()
{
int index1, index2;
char arr[num_row][num_column];
char letter = 'A';
char num = '1';
char in1, in2, again;
for (index1 =0; index1 < 7; index1++) // this loop assign A B C D to all 7 rows
{
for (index2 = 1; index2 <5; index2++)
{
arr[index1][index2] = letter;
letter++;
}
letter = 'A';
}
for (index1= 0; index1< 7; index1++) //This loop assign the number 1 to 7 to the 7 rows
{
arr[index1][0] = num;
num++;
}
for (index1 = 0; index1 < 7; index1++) // this loop shows the plane seats
{
for (index2 = 0; index2 < 5; index2++)
{
cout << arr[index1][index2] << " ";
}
cout << endl;
}
cout << endl;
do // this loop repeat the process
{
cout << "Please enter a seat to return, or N to exit.\n";
cin >> (in1); // getting first character
cin >> (in2); //getting second character
in2 = toupper(in2);
for (index1 = 0; index1 < 7; index1++) //this loop mark X to a seat
{
if (in1 == arr[index1][0])
{
for (index2 = 1; index2 < 5; index2++)
{
if (in2 == arr[index1][index2])
{arr[index1][index2] = 'X'; break; } // Note: break required
elseif (in1 != arr[index1][0]) // Note: in1 instead of in2
{
cout << "Seat is taken. Please try again.\n";
break; // Note: break required
}
// Note: check for invalid input/'N' before you start this loop
}
break;
}
}
for (index1 = 0; index1 < 7; index1++) //This loop shows whats happen after the seat is marked
{
{
for (index2 = 0; index2 < 5; index2++)
{
cout << arr[index1][index2] << " ";
}
cout << endl;
}
}
cout << "Please enter Y, then enter another seat to return, or N to exit.\n";
cin >> again;
}while (toupper(again) == 'Y');
}
Because it is the wrong place for it. Within the loop on line 50 you search for the row. The else on line 60 in your OP would then state "Invalid input.[...]" six times even though it is a valid seat. Hence you cannot use that else which means you need to check the validity before the loop.
If you'd use the calculation I showed above you wouldn't need the loop at all and just the validity check.