c++ 为什么cin.get()和cin.ignore()会导致程序暂停?

q9yhzks0  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(124)

因此,我试图制作一个井字游戏,并找到了一个使用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
***************************************************/

抱歉,我的程序中有任何坏习惯,我对这方面还比较陌生。只是些样品。再次,我的问题是为什么这两个一起使用时,导致程序暂停?

cngwdvgl

cngwdvgl1#

ignore()表示ignore是cin流的成员函数
阅读a little documentation,它有原型

cin.ignore( streamsize count = 1, int_type delim = EOF );

该函数具有默认参数,由于您没有指定新参数,因此它将忽略输入中的一个字符。然后下一个输入将被cin.get()捕获
但是,cin.get()并没有指定它应该对输入做什么,所以您应该将其写成cin.get(myvar);myvar = cin.get();。也许递归调用piecePlacer与新的输入?

dy2hfwbg

dy2hfwbg2#

我想另一个答案并没有回答“为什么程序会暂停”,所以让我们继续。
cin.ignore()使用默认参数count=1和delim=EOF,因此下一次击键将被忽略,即使它是EOF。
cin.get()是暂停程序,使其等待输入。
这个输入(通常是指下一次击键),当它到来时,将被立即忽略,程序将继续。
例如:

cin.ignore();
    cin.get();
    return 0;

上面的这个将暂停,等待输入,当它到来时,将返回/结束程序。

相关问题