该计划的目的是为这样的乘客分配座位。
1 ABCD
2 AXCD
3 ABCD
4 ABXD
5 ABCD
6 ABCD
7 ABCD
我的程序在 cin.get() 中获得了一个换行符。我该如何解决?
有关详细信息,请参阅程序中的注释。如果你需要更多细节,我会给你更多。
#include <iostream>
#include <cstring>
using namespace std;
void display_seats(char& column, char& row, char seat[][5]);
void program(char seats[][5], char& row, char& column, bool& allfull);
int main()
{
char seats[7][5] = { '1', 'A', 'B', 'C', 'D', '2', 'A', 'B', 'C', 'D', '3', 'A', 'B', 'C', 'D', '4', 'A', 'B', 'C', 'D', '5', 'A', 'B', 'C', 'D', '6', 'A', 'B', 'C', 'D', '7', 'A', 'B', 'C', 'D' };
char column, row, ans, ans2;
bool allfull = true;
cout << "Flight-078\t\t\t Authentification number: 38583305324556\n\n";
do
{
program(seats, row, column, allfull);
if (allfull)
{
cout << "\nThe Airplane is full. Press return to exit.\n";
cin.get(ans2); //same here, the \n from input get stored here and program closes automatically.
break;
}
else
{
cout << "\nThere are seats available. Do you want to continue assigning seats?(Y/N)\n";
cin >> ans;
}
} while (ans == 'y' || ans == 'Y');
return 0;
}
void display_seats(char& column, char& row, char seat[][5])
{
cout << "\nThese are the seats that are available:\n" << endl;
for (int i = 0; i < 7; i++){
for (int j = 0; j < 5; j++) {
cout << seat[i][j] << " ";
if (j == 4)
cout << endl;
}
}
cout << "\n\nEnter a seat to use: \n";
do {
cin.get(row); //when the program repeats add a '\n' here, from the do-while of the main function (I think)
cin.get(column);
if (row == '\n' || column == '\n') //covering the error. I want to take this out.
cout << "Please confirm the seat by entering it again:\n"; //and this
} while (row == '\n' || column == '\n');
return;
}
void program(char seats[][5], char& row, char& column, bool& allfull)
{
int k = 0;
bool passed, err = false;
char mark = 'X';
display_seats(column, row, seats);
cout << endl << endl;
passed = 0;
for (int i = 0; i < 7; i++)
{
if ((row < '8' && row > '0') && (column == 'A' || column == 'B' || column == 'C' || column == 'D')){
if (row == seats[i][k]){
for (k = 0; column != seats[i][k] && k < 5; k++);
if (column == seats[i][k] && !passed && seats[i][k] != mark)
{
seats[i][k] = mark;
passed = true;
}
else
{
for (k = 0; seats[i][k] != mark && k < 5; k++);
if (seats[i][k] == mark)
{
cout << "\n********************************************************\n";
cout << "\nSorry. That seat is already taken. Try choosing another.\n";
cout << "\n********************************************************\n\n";
program(seats, row, column, allfull);
}
}
}
}
else
{
err = true;
cout << "\n****************************\n";
cout << "\n\tWrong input!\n";
cout << "\n****************************\n\n";
program(seats, row, column, allfull);
break;
}
}
for (int i = 0; i < 7; i++){
if (err)
break;
for (int j = 0; j < 5; j++) {
if (seats[i][j] != 'X')
allfull = 0;
cout << seats[i][j] << " ";
if (j == 4)
cout << endl;
}
}
}