简体   繁体   中英

Why does cin.get() and cin.ignore() cause program to pause?

So I'm trying to make a tic tac toe game and found a sample code using cin.get() and cin.ignore() to pause the program. The question I found regarding these two were explanations on what they do.

So far, what I know about these two is, cin.get() can be use to grab the first letter of a variable and cin.ignore() will ignore at least the first character in a variable.

Here's the sample I found:

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

Here's my unfinished tic tac toe program, using .ignore and .get at lines 66 and 67 (the one with //****...), it's used to pause the program when it outputs "invalid input" when asking for a move.

#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
***************************************************/

Apologies for any bad habits in my program, I'm still relatively new at this. It's just samples. Again, my question is why does these two when used together, causes the program to pause?

cin.ignore() means ignore is a member function of the cin stream

Reading a little documentation , it has the prototype

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

The function has default parameters, and as you're not specifying new ones, it'll ignore one character in your input. Then the next input will be captured by cin.get()

However, cin.get() does not specify what it should do with the input, so you should write it as cin.get(myvar); or myvar = cin.get(); .. perhaps recursively call piecePlacer with the new input?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM