我在使用_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'
,'2'
和'3'
而不是整数1
,2
和3
进行比较。
switch(a){
case '1':
play();
break;
case '2':
// delete();
break;
case '3':
// help();
break;
return 0;
}
关于c++ - _getch不将输入读入变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10491483/