emanuel1337
emanuel1337

Reputation: 57

My program is getting \n via cin.get from previous input

The purpose of the program is to assign seats to passagers like this.

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

My program is getting a new line character in the cin.get(). How can I fix it?

See comments in the program for details. If you need more details, I'll give you more.

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

Upvotes: 1

Views: 864

Answers (1)

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 154035

You are mixing formatted input (i.e., using >>) with unformatted input (e.g. using get()). These behave quite differently: formatted input starts off with skipping leading whitespace, reads its content and stops immediately when done. For example, if you read a characters using

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

this skips any spaces, reads a character, and stops (if it can't read a character, e.g., because it reaches the end of the stream, the input fails and the stream converts to false: you need to always check after your input that it was successful). If you asked for a character and the user entered the character followed by the enter key, the enter key actually put a '\n' into the stream! That is still there waiting to be read immediately.

You normally want to ignore characters already in the stream when switching between formatted input and unformatted input. Unfortunately, neither you nor the system can know how many characters are actually waiting already. A typical approach is, thus, to ignore all characters until the end of the line:

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

This somewhat magical incantation uses a special value to indicate that an arbitrary amount of characters should be ignored until a '\n' character is found: this end character will be the last character to be ignored.

Upvotes: 1

Related Questions