简体   繁体   English

无法使_getch和Switch Case在C ++中协同工作

[英]Can't get _getch and Switch Case to work togeather in C++

I'm kinda new to C++ and I'm having an hard time with a little ""game"" I'm making. 我是C ++的新手,我在制作一些“游戏”时遇到了困难。

Here I'm tryng to create a sort of menu that you can move through using the Arrow keys as inputs, and confirm using the Enter key, but despite not getting any error in the compiler the program just closes itself when I reach the second Switch Case. 在这里,我尝试创建一种菜单,您可以使用箭头键作为输入来移动菜单,并使用Enter键进行确认,但是尽管在编译器中未出现任何错误,但是当我到达第二个Switch时,程序只会自行关闭案件。

What am I doing wrong? 我究竟做错了什么? Thanks 谢谢

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <windows.h>
#include <conio.h>


#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77
#define ENTER 13



using namespace std;


int main()
{
    struct Stats {
        int Hp;
    int Atk;
    int Speed;
    int Defense;
};

struct Defense {
    bool IsWeakFire;
    bool IsWeakIce;
    bool IsWeakWind;
    bool IsWeakElectric;
    bool IsWeakLight;
    bool IsWeakDark;
};


struct CharacterStats {
    string Name;
    string Desc;
    string Persona;
    Stats Stats;
    Defense Defense;
}Characters[4];


//Program Start

cout << "You are now playng: ProjectPB \n" << endl <<
        "MC Name> ";

cin >> Characters[0].Name;

cout << "\n \n The game will now start.";
for (int i=0;i<4;i++)
{
    Sleep(500);
    cout << ".";
}

system("cls");


//Fight start




int i = 0;
int sel = 1;
int c = 0;

while (i == 0) {
    switch (sel)
    {
    case 1:
        system("cls");
        cout << " |ATTACK|      PERSONA         DEFEND" << endl;
        system("pause");
        break;

    case 2:
        system("cls");
        cout << "  ATTACK       |PERSONA|       DEFEND" << endl;
        system("pause");
        break;

    case 3:
        system("cls");
        cout << "  ATTACK       PERSONA        |DEFEND|" << endl;
        system("pause");
    }       break;

    Sleep(100);


int i = 0;
int sel = 1;


    while (i == 0){
        switch (sel)
        {
        case 1:
            system("cls");
            cout << " |ATTACK|      PERSONA         DEFEND" << endl;
            system("pause");
            break;

        case 2:
            system("cls");
            cout << "  ATTACK       |PERSONA|       DEFEND" << endl;
            system("pause");
            break;

        case 3:
            system("cls");
            cout << "  ATTACK       PERSONA        |DEFEND|" << endl;
            system("pause");
        }       break;

        Sleep(100);





        int c = _getch();
        switch (c)
        {
        case KEY_LEFT :
            sel--;
            if (sel <= 0)
            {
                sel = 3;
            }
            break;

        case KEY_RIGHT :
                sel++;
                if (sel > 3)
                {
                    sel = 1;
                }
                break;


        case ENTER:
                    i = 1;
                    break;
        }



    }







}



return 0;
}

This must be because of the non-empty input buffer during this statement 这一定是由于该语句期间的非空输入缓冲区

int c = _getch(); int c = _getch();

To solve this,simply clear the buffer with this right before getch() 要解决此问题,只需在getch()之前使用此权限清除缓冲区

while ((getchar()) != '\\n'); while((getchar())!='\\ n');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM