c ++ switch语句

时间:2016-04-09 17:05:17

标签: c++ switch-statement

我在c ++中遇到了switch语句的问题。

我已经建立了一个菜单,我可以在其中选择要做的各种事情,当我选择一个不需要设置任何变量的函数,即getstat() - 调用stats,它工作正常。但是每次我选择一个带变量的函数时,代码都会完美运行,但是当它循环回来进行下一次选择时,它会坚持以前选择的内容。

有关如何解决这个问题的任何想法?

在下面的代码中,我检查我的统计数据(工作正常),然后选择踢球,输入所需的距离,该功能踢球,然后我想选择检查距离,但是当我尝试已经选中的踢球时球。

我认为它与cin有关,因为其他功能没有cin并且它们完美地工作。

#include <iostream>
#include <string>
#include <windows.h>

int distance = 0;
int target;
using namespace std;

int main() {
    string Menu[3] = { "Check stats", "kick the ball", "Check distance" };
int pointer = 0;

while (true)
{
    system("cls");

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
    cout << "Main Menu\n\n";

    for (int i = 0; i < 3; ++i)
    {
        if (i == pointer)
        {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
            cout << Menu[i] << endl;
        }
        else
        {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
            cout << Menu[i] << endl;
        }
    }

    while (true)
    {
        if (GetAsyncKeyState(VK_UP) != 0)
        {
            pointer -= 1;
            if (pointer == -1)
            {
                pointer = 2;
            }
            break;
        }
        else if (GetAsyncKeyState(VK_DOWN) != 0)
        {
            pointer += 1;
            if (pointer == 4)
            {
                pointer = 0;
            }
            break;
        }
        else if (GetAsyncKeyState(VK_RETURN) != 0)
        {
            switch (pointer)
            {
            case 0:
            {
                cout << "\n\n\nPlaying Check stats";
                cout << me.stats();
                Sleep(1000);
            } break;
            case 1:
            {
                cout << "\n\n\n Kick the ball \n how far?";
                cin >> distance; 
                target = distance;
                me.kick();
                Sleep(1000);
            } break;
            case 2:
            {
                cout << "\n\n\nCheck distance";
                me.distance();
                Sleep(1000);
            } break;

            case 3:
            {
                return 0;
            } break;
            }
            break;
        }
    }

    Sleep(150);
}

return 0;

}

1 个答案:

答案 0 :(得分:1)

原因是你从键盘输入一个值后,你必须输入(VK_RETURN)cin来读取值,并根据MSDN上GetAsyncKeyState()函数的文档:

  

如果函数成功,则返回值指定是否为键   自上次调用GetAsyncKeyState以来是否被按下,以及是否   密钥当前上升或下降。如果设置了最高位,则   key为down,如果设置了最低有效位,则键为   在上一次调用GetAsyncKeyState之后按下。

这意味着函数调用将返回true,即使在上一次调用后按下最后一次按键,所以在按Enter键获取带cin的输入后,下次出现时else if (GetAsyncKeyState(VK_RETURN) != 0)将为真(这次没有按任何键)并要求你输入距离......依此类推......

你可以做这样的事情(无论如何不是获得你想要完成的事情的最佳解决方案):

int flag=1;
while (true) {
system("cls");

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
cout << "Main Menu\n\n";

for (int i = 0; i < 7; ++i)
{
    if (i == pointer)
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
        cout << Menu[i] << endl;
    }
    else
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
        cout << Menu[i] << endl;
    }
}
if (GetAsyncKeyState(VK_UP) != 0){
        pointer -= 1;
        if (pointer == -1)
        {
            pointer = 2;
        }
        flag=1;
        //break;//i don't know why you need this one
    }
else if (GetAsyncKeyState(VK_DOWN) != 0) {
        pointer += 1;
        if (pointer == 7)
        {
            pointer = 0;
        }
        flag=1;
        //break;//i don't know why you need this one
    }
else if (GetAsyncKeyState(VK_RETURN) != 0 && flag){
        switch (pointer) {
        case 0: cout << "\n\n\n Call stats!";
            me.stats(); //this works perfectly and I can 
            Sleep(1000) //select a different option on the next loop.
        break;
        case 1: cout << "\n\n\n Kick the ball \n how far in meters?";
            FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
            cin >> choice; //previously defined
            flag=0;//makes the next else if(GetAsyncKeyState(VK_RETURN) != 0 && flag) false,otherwise since enter(VK_RETURN) will be pressed to get input using cin,you will enter in an endless loop which leads only to here...
            Sleep(1000);
          break;
        case 2: cout << "\n\n\n Check distance";
            me.distance();
            Sleep(1000);
        break;
        case 3: return 0; //Exit
         break;
        }
        //break;//i don't know why you need this one
        }
        else
           flag=1;
    }
  Sleep(150);

  }