1

该计划的目的是为这样的乘客分配座位。

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

1 回答 1

1

您正在混合格式化输入(即 using >>)和未格式化输入(例如 using get())。它们的行为完全不同:格式化输入从跳过前导空格开始,读取其内容并在完成后立即停止。例如,如果您使用

if (std::cin >> ans) { ... }

这会跳过任何空格,读取一个字符并停止(如果它无法读取一个字符,例如,因为它到达流的末尾,输入失败并且流转换为false:您需要在输入后始终检查它是成功的)。如果您要求输入一个字符,并且用户输入了该字符,然后按 enter 键,则 enter 键实际上将 a'\n'放入流中!那仍然在那里等待立即阅读。

在格式化输入和未格式化输入之间切换时,您通常希望忽略流中已经存在的字符。不幸的是,您和系统都不知道实际上有多少字符在等待。因此,一种典型的方法是忽略所有字符,直到行尾:

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

这个有点神奇的咒语使用一个特殊的值来指示在找到一个字符之前应该忽略任意数量的字符'\n':这个结束字符将是最后一个被忽略的字符。

于 2013-11-14T01:22:26.040 回答