gpt4 book ai didi

c++ - 我的程序正在通过 cin.get 从之前的输入获取\n

转载 作者:太空宇宙 更新时间:2023-11-04 11:45:34 26 4
gpt4 key购买 nike

程序的目的就是这样给乘客分配座位。

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

最佳答案

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

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

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

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

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

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

关于c++ - 我的程序正在通过 cin.get 从之前的输入获取\n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19967315/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com