_getch不将输入读入变量

_getch not reading input into variable

本文关键字:变量 输入 getch      更新时间:2023-10-16

我在使用 _getch() 函数时遇到问题,我希望它使用户在从菜单中选择内容时无需按 ENTER。但是,当我尝试使用它时,它要么不将数据输入到变量中,要么跳过我拥有的开关。我使用的是Windows 7和CodeBlocks IDE。我做错了什么?提前谢谢。

#include <iostream>
#include <sstream>
#include <conio.h>
using namespace std;
stringstream ss;
int a;
void play()
{
  cout << "nYou wake up on the forest floor. You do not remember where you are, who you are, or anythingnthat has happened before you waking up. You seem to be some type of...n";
  cout << "--CHARACTER SELECTION--n1. Warriorn2. Magen3. Rouge";
  cin.get();
};

int main()
{
//  CreateDirectory()
  cout << "--SELECTION MENU--n1. Beginn2. Delete Gamen3. Instructions" << endl;
  a=_getch();

  switch(a){
  case 1:
  play();
  break;
  case 2:
 // delete();
  break;
  case 3:
 // help();
  break;
  return 0;
  }
}

将您的字符与字符'1''2''3'进行比较,而不是整数123 进行比较。

switch(a){
  case '1':
  play();
  break;
  case '2':
 // delete();
  break;
  case '3':
 // help();
  break;
  return 0;
}