我的程序通过以前输入的cin.get获得\ n

时间:2013-11-14 00:35:07

标签: c++ get cin

该计划的目的是为这样的通道分配席位。

1 A B C D
 2 A X C D
 3 A B C D
 4 A B X D
 5 A B C D
 6 A B C D
 7 A B C D

我的程序在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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

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

读取字符
if (std::cin >> ans) { ... }

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

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

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

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