How to clear the enter buffer in C++

Viewed 76

So I have a menu that uses arrow keys to navigate on the options, The thing is when I chose an option using enter key, the function gets called but it does not go back to the menu but rather calls the function again. Here is my code

int menu(){
 string Menu[6] = {"Insert Student Record", "Delete Student Record", "Update Student Record", "Display All Student Record", "Exit & Save"};
 int pointer = 0;
 HANDLE  hConsole;
 hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

 while(true){
    system("cls");
    SetColor(11);cout<<"\n\tPlease navigate to the menu using up-down arrow keys"<<endl;SetColor(15);
    for(int i=0; i<5; ++i){
        if(i==pointer){
             SetConsoleTextAttribute(hConsole, 159);
             cout << Menu[i] << endl;
        }else{
            SetConsoleTextAttribute(hConsole, 15); 
            cout << Menu[i] << endl;
        }
        SetConsoleTextAttribute(hConsole, 15);

     }
    
     while(true){
        if(GetAsyncKeyState(VK_UP) != 0){
            --pointer;
            if(pointer == -1){
                pointer = 4;
            }
            break;
        }else if(GetAsyncKeyState(VK_DOWN) != 0){
            ++pointer;
            if(pointer == 5){
                pointer = 0;
            }
            break;
        }else if(GetAsyncKeyState(VK_RETURN) != 0){
            switch(pointer){
            case 0:
            {
                system("cls");
                insertMode();
            }break;
            case 1:
            {
                system("cls");
                deleteMode();
            }break;
            case 2:
            {
                system("cls");
                updateMode();
                cout<<"\n\t";
            }break;
            case 3:
            {
                system("cls");
                studList.display();
                cout<<"\n\t";system("pause");
            }break;
            case 4:
            {
                system("cls");
                exitMessage();
                studList.save();
                exit(0);
            } break;
          }
          break;
        }
     }
      Sleep(150);
    }
}

Any help will be greatly appreciated. Thank you very much

0 Answers
Related