[英]Cin.get() does not wait for a keypress even with cin.ignore() before it
[英]Why does cin.get() and cin.ignore() cause program to pause?
因此,我试图制作一个井字游戏,并使用cin.get()和cin.ignore()暂停了程序,找到了示例代码。 我发现的关于这两个的问题是它们的作用的解释。
到目前为止,我对这两者的了解是,cin.get()可用于获取变量的第一个字母,而cin.ignore()将至少忽略变量中的第一个字符。
这是我发现的示例:
// istream::ignore example
#include <iostream> // std::cin, std::cout
int main () {
char first, last;
std::cout << "Please, enter your first name followed by your surname: ";
first = std::cin.get(); // get one character
std::cin.ignore(256,' '); // ignore until space
last = std::cin.get(); // get one character
std::cout << "Your initials are " << first << last << '\n';
return 0;
}
这是我未完成的井字游戏程序,在第66和67行使用.ignore和.get(带有// **** ...的代码),当请求时输出“无效输入”时,它会暂停程序一个动作。
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
string k;
int scoreP1, scoreP2;
char cell[10] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
void board(/*int scoreP1, int scoreP2*/);
void piecePlacer(int& pMover, char& piece);
int main()
{
int player, i = 1, pMove, choice, j;
char piece;
do{
cout << "Tic-Tac-Toe\n\n"
<< "[1] Player vs Player\n"
<< "[2] Player vs Computer\n"
<< "[3] Exit";
cin >> choice;
while (choice == 1){
do{
system("cls"); // fix (change)
board(/*0,0*/);
player = (i % 2) ? 1 : 2; // when i % 2 == 1 (true), player = 1; when i % 2 == 0 (false), player = 2
piece = (player == 1) ? 'X' : 'O';
cout << "\n\t Player " << player << ", it's your turn ";
cin >> pMove;
piecePlacer(pMove, piece);
i++;
} while (pMove == 0); // piecePlacer initializes pMove to 0 when user enters invalid number
}
} while (choice != 3);
return 0;
}
void piecePlacer(int& pMove, char& piece){
if (pMove == 1 && cell[1] == ' ')
cell[1] = piece;
else if (pMove == 2 && cell[2] == ' ')
cell[2] = piece;
else if (pMove == 3 && cell[3] == ' ')
cell[3] = piece;
else if (pMove == 4 && cell[4] == ' ')
cell[4] = piece;
else if (pMove == 5 && cell[5] == ' ')
cell[5] = piece;
else if (pMove == 6 && cell[6] == ' ')
cell[6] = piece;
else if (pMove == 7 && cell[7] == ' ')
cell[7] = piece;
else if (pMove == 8 && cell[8] == ' ')
cell[8] = piece;
else if (pMove == 9 && cell[9] == ' ')
cell[9] = piece;
else{
cout << "\n\t\t Invalid Move.";
pMove = 0;
cin.ignore(); //*************************************************
cin.get(); //*************************************************
}
}
void board(/*int scoreP1, int scoreP2*/){
cout << "\n\n\t\t P1 [" << scoreP1 << "]" << " P2 [" << scoreP2 << "]"; // fix (undefined)
cout << "\n\n\n\n\n";
cout << "\t\t | | \t\t\tCell orienation:" << endl;
cout << "\t\t " << cell[1] << " | " << cell[2] << " | " << cell[3] << endl;
cout << "\t\t_____|_____|_____\t\t\t 1 2 3" << endl;
cout << "\t\t | | " << endl;
cout << "\t\t " << cell[4] << " | " << cell[5] << " | " << cell[6] << "\t\t\t\t 4 5 6" << endl;
cout << "\t\t_____|_____|_____" << endl;
cout << "\t\t | | \t\t\t 7 8 9" << endl;
cout << "\t\t " << cell[7] << " | " << cell[8] << " | " << cell[9] << endl;
cout << "\t\t | | " << endl;
}
/***************************************************
for function 'piecePlacer'
pMove== 0 - invalid move
***************************************************/
对于我程序中的任何不良习惯,我深表歉意,但是我在这个方面还比较陌生。 这只是样本。 再次,我的问题是为什么这两个一起使用会导致程序暂停?
cin.ignore()表示ignore是cin流的成员函数
阅读一些文档 ,它具有原型
cin.ignore( streamsize count = 1, int_type delim = EOF );
该函数具有默认参数,并且由于您未指定新参数,因此它将忽略输入中的一个字符。 然后,下一个输入将由cin.get()捕获
但是,cin.get()未指定应如何处理输入,因此应将其写为cin.get(myvar);
或myvar = cin.get();
..也许用新的输入递归调用piecePlacer?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.