所以我正在尝试制作一个tic tac toe游戏,并使用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;
}
这是我未完成的tic tac toe程序,使用.ignore和.get在第66和67行(带有// ****的那个......),它用于在输出“无效输入”时暂停程序当要求搬家时。
#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
***************************************************/
对于我的计划中的任何坏习惯道歉,我仍然相对较新。这只是样品。同样,我的问题是为什么这两个一起使用会导致程序暂停?
答案 0 :(得分:0)
cin.ignore()表示ignore是cin stream的成员函数
阅读a little documentation,它有原型
cin.get(myvar);
该函数具有默认参数,因为您没有指定新参数,它将忽略输入中的一个字符。 然后cin.get()
将捕获下一个输入但是,cin.get()没有指定它应该对输入做什么,所以你应该把它写成myvar = cin.get();
或{{1}} ..也许用新输入递归调用piecePlacer? / p>