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

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

因此,我试图制作一个井字游戏,并找到了一个使用cin.get()和cin.ignore()暂停程序的示例代码。我发现关于这两个问题的解释是他们做什么。
到目前为止,我对这两个函数的了解是,cin.get()可以用来抓取变量的第一个字母,cin.ignore()至少会忽略变量的第一个字符。
以下是我找到的样本:

  1. // istream::ignore example
  2. #include <iostream> // std::cin, std::cout
  3. int main () {
  4. char first, last;
  5. std::cout << "Please, enter your first name followed by your surname: ";
  6. first = std::cin.get(); // get one character
  7. std::cin.ignore(256,' '); // ignore until space
  8. last = std::cin.get(); // get one character
  9. std::cout << "Your initials are " << first << last << '\n';
  10. return 0;
  11. }

这是我未完成的井字游戏程序,在第66行和第67行使用.ignore和.get(有//****......),当程序请求移动时输出“无效输入”时,它用来暂停程序。

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string>
  4. using namespace std;
  5. string k;
  6. int scoreP1, scoreP2;
  7. char cell[10] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
  8. void board(/*int scoreP1, int scoreP2*/);
  9. void piecePlacer(int& pMover, char& piece);
  10. int main()
  11. {
  12. int player, i = 1, pMove, choice, j;
  13. char piece;
  14. do{
  15. cout << "Tic-Tac-Toe\n\n"
  16. << "[1] Player vs Player\n"
  17. << "[2] Player vs Computer\n"
  18. << "[3] Exit";
  19. cin >> choice;
  20. while (choice == 1){
  21. do{
  22. system("cls"); // fix (change)
  23. board(/*0,0*/);
  24. player = (i % 2) ? 1 : 2; // when i % 2 == 1 (true), player = 1; when i % 2 == 0 (false), player = 2
  25. piece = (player == 1) ? 'X' : 'O';
  26. cout << "\n\t Player " << player << ", it's your turn ";
  27. cin >> pMove;
  28. piecePlacer(pMove, piece);
  29. i++;
  30. } while (pMove == 0); // piecePlacer initializes pMove to 0 when user enters invalid number
  31. }
  32. } while (choice != 3);
  33. return 0;
  34. }
  35. void piecePlacer(int& pMove, char& piece){
  36. if (pMove == 1 && cell[1] == ' ')
  37. cell[1] = piece;
  38. else if (pMove == 2 && cell[2] == ' ')
  39. cell[2] = piece;
  40. else if (pMove == 3 && cell[3] == ' ')
  41. cell[3] = piece;
  42. else if (pMove == 4 && cell[4] == ' ')
  43. cell[4] = piece;
  44. else if (pMove == 5 && cell[5] == ' ')
  45. cell[5] = piece;
  46. else if (pMove == 6 && cell[6] == ' ')
  47. cell[6] = piece;
  48. else if (pMove == 7 && cell[7] == ' ')
  49. cell[7] = piece;
  50. else if (pMove == 8 && cell[8] == ' ')
  51. cell[8] = piece;
  52. else if (pMove == 9 && cell[9] == ' ')
  53. cell[9] = piece;
  54. else{
  55. cout << "\n\t\t Invalid Move.";
  56. pMove = 0;
  57. cin.ignore(); //*************************************************
  58. cin.get(); //*************************************************
  59. }
  60. }
  61. void board(/*int scoreP1, int scoreP2*/){
  62. cout << "\n\n\t\t P1 [" << scoreP1 << "]" << " P2 [" << scoreP2 << "]"; // fix (undefined)
  63. cout << "\n\n\n\n\n";
  64. cout << "\t\t | | \t\t\tCell orienation:" << endl;
  65. cout << "\t\t " << cell[1] << " | " << cell[2] << " | " << cell[3] << endl;
  66. cout << "\t\t_____|_____|_____\t\t\t 1 2 3" << endl;
  67. cout << "\t\t | | " << endl;
  68. cout << "\t\t " << cell[4] << " | " << cell[5] << " | " << cell[6] << "\t\t\t\t 4 5 6" << endl;
  69. cout << "\t\t_____|_____|_____" << endl;
  70. cout << "\t\t | | \t\t\t 7 8 9" << endl;
  71. cout << "\t\t " << cell[7] << " | " << cell[8] << " | " << cell[9] << endl;
  72. cout << "\t\t | | " << endl;
  73. }
  74. /***************************************************
  75. for function 'piecePlacer'
  76. pMove== 0 - invalid move
  77. ***************************************************/

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

cngwdvgl

cngwdvgl1#

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

  1. 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()是暂停程序,使其等待输入。
这个输入(通常是指下一次击键),当它到来时,将被立即忽略,程序将继续。
例如:

  1. cin.ignore();
  2. cin.get();
  3. return 0;

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

相关问题