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

标签 c++

所以我正在尝试制作井字游戏,并找到了使用 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
***************************************************/

对于我程序中的任何不良习惯,我深表歉意,我在这方面还比较陌生。这只是 sample 。同样,我的问题是为什么这两个一起使用时会导致程序暂停?

最佳答案

cin.ignore()表示ignore是cin流的一个成员函数

阅读 a little documentation , 它有原型(prototype)

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

该函数有默认参数,并且由于您没有指定新参数,它会忽略您输入的一个字符。 然后下一个输入将被 cin.get() 捕获

然而,cin.get() 没有指定它应该对输入做什么,所以你应该把它写成 cin.get(myvar);myvar = cin.get (); .. 也许用新的输入递归调用 piecePlacer?

关于c++ - 为什么 cin.get() 和 cin.ignore() 会导致程序暂停?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30778413/

相关文章:

c++ - 使用字符数组和递归来反转字符串,一些线程1:EXC_BAD_ACCESS错误

c++ - cin.get() 是非阻塞的

c++ - 为什么c中有些函数名要写成两行?

android - 使用 Android NDK 使用 clang++ 编译 C++ 代码时未定义对 `_Unwind_Resume' 的引用

c++ - std::streampos 是否保证为 unsigned long long?

为 32 位和 64 位处理器强制对齐的 C++ 安全性

c++ - 为什么 "C++ Core Guidelines"推荐首选独立函数而不是类成员?

c++ - 具有成员函数和携带参数的构造函数的多线程

c++ - 构造函数存储的内容与传入的内容不同

c++ - VS13 中的 IntelliSense 愚蠢错误