_getch没有将输入读入变量

时间:2012-05-08 01:49:52

标签: c++ getch

我遇到了_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 anything\nthat has happened before you waking up. You seem to be some type of...\n";
  cout << "--CHARACTER SELECTION--\n1. Warrior\n2. Mage\n3. Rouge";
  cin.get();
};


int main()
{
//  CreateDirectory()
  cout << "--SELECTION MENU--\n1. Begin\n2. Delete Game\n3. Instructions" << endl;
  a=_getch();


  switch(a){

  case 1:
  play();
  break;

  case 2:
 // delete();
  break;

  case 3:
 // help();
  break;
  return 0;
  }
}

1 个答案:

答案 0 :(得分:1)

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

switch(a){

  case '1':
  play();
  break;

  case '2':
 // delete();
  break;

  case '3':
 // help();
  break;
  return 0;
}