为什么switch总是运行default? (有休息;包括) [英] Why does switch always run default? (with break; included)
问题描述
当我使用开关
语句如图所示。
从其他线程和论坛,我发现这可能与有关, getch()
,并返回 char
以及 \\\
。
我已经尝试了一个小时,但我不能解决这个问题。我使用 getch()
的原因是:
现在我的代码部分:
//设置游戏字段
generateField();
setPlayerStart();
// printGameInfo();要生产
//开始游戏while循环
int userInput;
do {
// system(cls);禁用测试目的
printField();
userInput = getch();
switch(userInput){
case 72:{// ARROW UP
cout< 1What< endl; // ALSO FOR TESTING PURPOSES
break;
}
case 80:{//向下
cout<< 2What< endl;
break;
}
case 75:{// ARROW LEFT
cout<< 3What< endl;
break;
}
case 77:{// ARROW RIGHT
cout< 4What< endl;
break;
}
case 113:{//q
return false; //退出游戏
}
默认值:{
cout< Default ...< endl;
}
}
} while(userInput!= 5);
由于这是Windows,您可以使用 ReadConsoleInput
以读取关键事件。我把这些部分分成了函数,但我不认为 handleInput
的返回语义是非常棒的。
#include< iostream>
#include< stdexcept>
#include< windows.h>
HANDLE getStdinHandle(){
HANDLE hIn;
if((hIn = GetStdHandle(STD_INPUT_HANDLE))== INVALID_HANDLE_VALUE){
throw std :: runtime_error(无法获取标准输入句柄。
}
return hIn;
}
WORD readVkCode(HANDLE hIn){
INPUT_RECORD rec;
DWORD numRead;
while(ReadConsoleInput(hIn,& rec,1,& numRead)&& numRead == 1){
if(rec.EventType == KEY_EVENT&& rec.Event。 KeyEvent.bKeyDown){
return rec.Event.KeyEvent.wVirtualKeyCode;
}
}
throw std :: runtime_error(无法读取输入。
}
bool handleInput(WORD vk){
switch(vk){
case VK_UP:
std :: cout< up\\\
;
break;
case VK_DOWN:
std :: cout<< down\\\
;
break;
case VK_LEFT:
std :: cout<< left\\\
;
break;
case VK_RIGHT:
std :: cout<< right\\\
;
break;
case'Q'://它是Windows; ASCII是安全的
return true;
}
return false;
}
int main(){
try {
auto hIn = getStdinHandle();
while(auto vk = readVkCode(hIn)){
if(handleInput(vk)){
return 0;
}
}
} catch(const std :: exception& ex){
std :: cerr< ex.what();
return 1;
}
}
其他有用的链接:
C++ newbie trying to make a simple text game on a 2D array.
When I use the switch
statement as shown. It will always print the default value, regardless of what else happens.
From other threads and forums I found it probably has something to do with the getch()
and that it returns the char
as well as \n
.
I have tried for an hour now but I can not solve this. The reason I use getch()
is this: C++ change canonical mode in windows (for reference).
Part of my code now:
//Set up game field
generateField();
setPlayerStart();
//printGameInfo(); TO BE MADE
//Start game while loop
int userInput;
do{
//system("cls"); DISABLED FOR TESTING PURPOSES
printField();
userInput = getch();
switch(userInput){
case 72:{ //ARROW UP
cout << "1What" << endl; //ALSO FOR TESTING PURPOSES
break;
}
case 80:{ //ARROW DOWN
cout << "2What" << endl;
break;
}
case 75:{ //ARROW LEFT
cout << "3What" << endl;
break;
}
case 77:{ //ARROW RIGHT
cout << "4What" << endl;
break;
}
case 113:{ //"q"
return false; //Quit game
}
default:{
cout << "Default..." << endl;
}
}
} while(userInput != 5);
Since this is Windows, you can use ReadConsoleInput
to read key events. I've separated the parts into functions, but I don't really think the return semantics of handleInput
is all that great.
#include <iostream>
#include <stdexcept>
#include <windows.h>
HANDLE getStdinHandle() {
HANDLE hIn;
if ((hIn = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
throw std::runtime_error("Failed to get standard input handle.");
}
return hIn;
}
WORD readVkCode(HANDLE hIn) {
INPUT_RECORD rec;
DWORD numRead;
while (ReadConsoleInput(hIn, &rec, 1, &numRead) && numRead == 1) {
if (rec.EventType == KEY_EVENT && rec.Event.KeyEvent.bKeyDown) {
return rec.Event.KeyEvent.wVirtualKeyCode;
}
}
throw std::runtime_error("Failed to read input.");
}
bool handleInput(WORD vk) {
switch (vk) {
case VK_UP:
std::cout << "up\n";
break;
case VK_DOWN:
std::cout << "down\n";
break;
case VK_LEFT:
std::cout << "left\n";
break;
case VK_RIGHT:
std::cout << "right\n";
break;
case 'Q': //it's Windows; ASCII is safe
return true;
}
return false;
}
int main() {
try {
auto hIn = getStdinHandle();
while (auto vk = readVkCode(hIn)) {
if (handleInput(vk)) {
return 0;
}
}
} catch (const std::exception &ex) {
std::cerr << ex.what();
return 1;
}
}
Other useful links:
这篇关于为什么switch总是运行default? (有休息;包括)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!