选择错误时如何允许用户再次输入

时间:2019-07-14 00:54:08

标签: c++ visual-c++

当用户输入非整数或数字的内容时,如何允许用户再次重新输入音乐选择。我在代码中使用isdigit。谢谢

我尝试使用isalpha来检测用户何时输入字母。但这也不起作用。我也尝试使用while(x <1 || x> 18);太。我正在使用箭头键(上下)作为菜单输入。所以这里的代码有点长

class Music
{
  private:
    int song; //song selection
  public:   
    void Song_Name() //function for song selection
    {
      do
      {
        cout<<"                                                                 These are the music available:                "<<endl<<endl;
        cout<<"                                                                 1. American Teen (Khalid)                   "<<endl;
        cout<<"                                                                 2. All We Know (Chainsmokers)               "<<endl;
        cout<<"                                                                 3. Born To Be Yours (Kygo)                  "<<endl;
        cout<<"                                                                 4. Bodak Yellow (Cardi-B)                   "<<endl;
        cout<<"                                                                 5. Beach House (Chainsmokers)               "<<endl;
        cout<<"                                                                 6. Congratulations (Post Malone)            "<<endl;
        cout<<"                                                                 7. Coaster (Khalid)                         "<<endl;
        cout<<"                                                                 8. Danger (Migos & Marshmello)              "<<endl;
        cout<<"                                                                 9. Earth (Lil Dicky)                        "<<endl;
        cout<<"                                                                10. Eastside (benny blanco, Halsey, Khalid)  "<<endl;
        cout<<"                                                                11. Happier (Marhsmello)                     "<<endl;
        cout<<"                                                                12. Heaven (Avicii)                          "<<endl;
        cout<<"                                                                13. Heaven (Khalid)                          "<<endl;
        cout<<"                                                                14. Here With Me (Marshmello)                "<<endl;
        cout<<"                                                                15. Joy (Bastille)                           "<<endl;
        cout<<"                                                                16. lovely (Khalid & Billie Eilish)          "<<endl;
        cout<<"                                                                17. I Fall Apart (Post Malone)               "<<endl;
        cout<<"                                                                18. I Don't Belong In This Club (Why Don't We) "<<endl;
        cout<<endl<<endl;
        cout<<"Please enter the songs that you would like to listen: ";
        cin>>song;
        if(isdigit(song)==0) //check for alphabet input
        {
          cout<<"Invalid Input! Please Enter Digits Only "<<endl;
          system("pause");
          system("cls");
        }
      }while(isdigit(song)==0); //repeat again if alphabet is inputted
    }
};
  while(running)
  {

    system("pause>nul"); // the >nul bit causes it the print no message

    if(GetAsyncKeyState(VK_DOWN) && a != 11) //down button pressed
      {
        if(a==-1)
        {
          gotoXY(75,4);     cout << "Main Menu";
          gotoXY(100,5);  cout<< endl << "                                                             .................................. "<<endl;
          gotoXY(100,6);  cout<< endl << "                                                             : .............................. : "<<endl; 
          gotoXY(100,7);    cout<< endl << "                                                             : :                            : : "<<endl;
          gotoXY(100,8);    cout<< endl << "                                                             : :  1) Sort By Song's Name    : : "<<endl;
          gotoXY(100,9);    cout<< endl << "                                                             : :............................: :  "<<endl;                              
          gotoXY(100,10); cout<< endl << "                                                             : :                            : : "<<endl;
          gotoXY(100,11);   cout<< endl << "                                                             : :  2) Sort By Genres         : :  "<<endl;
          gotoXY(100,12);   cout<< endl << "                                                             : :                            : :  "<<endl;
          gotoXY(100,13); cout<< endl << "                                                             : :                            : :  "<<endl;
          gotoXY(100,14);   cout<< endl << "                                                             : :  3) Favourites List        : :  "<<endl;
          gotoXY(100,15);   cout<< endl << "                                                             : :                            : :  "<<endl;
          gotoXY(100,16); cout<< endl << "                                                             : :                            : :   "<<endl;
          gotoXY(100,17);   cout<< endl << "                                                             : :  4) Exit Program           : :  "<<endl;
          gotoXY(100,18);   cout<< endl << "                                                             : :............................: : "<<endl;
          gotoXY(100,19); cout<< endl << "                                                             :................................: "<<endl;
          a=0;
          menu_item=0;  
          continue;
        }
        if(a==0)
        {
          gotoXY(75,4);     cout << "Main Menu";
          gotoXY(100,5);  cout<< endl << "                                                             .................................. "<<endl;
          gotoXY(100,6);  cout<< endl << "                                                             : .............................. : "<<endl; 
          gotoXY(100,7);    cout<< endl << "                                                             : :                            : : "<<endl;
          gotoXY(100,8);    cout<< endl << "                                                             : :  1) Sort By Song's Name    : : "<<endl;
          gotoXY(100,9);    cout<< endl << "                                                             : :............................: :  "<<endl;                              
          gotoXY(100,10); cout<< endl << "                                                             : :                            : : "<<endl;
          gotoXY(100,11);   cout<< endl << "                                                             : :  2) Sort By Genres         : :  "<<endl;
          gotoXY(100,12);   cout<< endl << "                                                             : :............................: :  "<<endl;
          gotoXY(100,13); cout<< endl << "                                                             : :                            : :  "<<endl;
          gotoXY(100,14);   cout<< endl << "                                                             : :  3) Favourites List        : :  "<<endl;
          gotoXY(100,15);   cout<< endl << "                                                             : :                            : :  "<<endl;
          gotoXY(100,16); cout<< endl << "                                                             : :                            : :   "<<endl;
          gotoXY(100,17);   cout<< endl << "                                                             : :  4) Exit Program           : :  "<<endl;
          gotoXY(100,18);   cout<< endl << "                                                             : :............................: : "<<endl;
          gotoXY(100,19); cout<< endl << "                                                             :................................: "<<endl;          
          a=1;
          menu_item=1;
          continue;
        }
        if(a==1)
        {
          gotoXY(75,4);     cout << "Main Menu";
          gotoXY(100,5);  cout<< endl << "                                                             .................................. "<<endl;
          gotoXY(100,6);  cout<< endl << "                                                             : .............................. : "<<endl; 
          gotoXY(100,7);    cout<< endl << "                                                             : :                            : : "<<endl;
          gotoXY(100,8);    cout<< endl << "                                                             : :  1) Sort By Song's Name    : : "<<endl;
          gotoXY(100,9);    cout<< endl << "                                                             : :                            : :  "<<endl;                              
          gotoXY(100,10); cout<< endl << "                                                             : :                            : : "<<endl;
          gotoXY(100,11);   cout<< endl << "                                                             : :  2) Sort By Genres         : :  "<<endl;
          gotoXY(100,12);   cout<< endl << "                                                             : :............................: :  "<<endl;
          gotoXY(100,13); cout<< endl << "                                                             : :                            : :  "<<endl;
          gotoXY(100,14);   cout<< endl << "                                                             : :  3) Favourites List        : :  "<<endl;
          gotoXY(100,15);   cout<< endl << "                                                             : :............................: :  "<<endl;
          gotoXY(100,16); cout<< endl << "                                                             : :                            : :   "<<endl;
          gotoXY(100,17);   cout<< endl << "                                                             : :  4) Exit Program           : :  "<<endl;
          gotoXY(100,18);   cout<< endl << "                                                             : :............................: : "<<endl;
          gotoXY(100,19); cout<< endl << "                                                             :................................: "<<endl;  
          a=2;
          menu_item=2;
          continue;
        }
        if(a==2)
        {

          gotoXY(75,4);     cout << "Main Menu";
          gotoXY(100,5);  cout<< endl << "                                                             .................................. "<<endl;
          gotoXY(100,6);  cout<< endl << "                                                             : .............................. : "<<endl; 
          gotoXY(100,7);    cout<< endl << "                                                             : :                            : : "<<endl;
          gotoXY(100,8);    cout<< endl << "                                                             : :  1) Sort By Song's Name    : : "<<endl;
          gotoXY(100,9);    cout<< endl << "                                                             : :                            : :  "<<endl;                              
          gotoXY(100,10); cout<< endl << "                                                             : :                            : : "<<endl;
          gotoXY(100,11);   cout<< endl << "                                                             : :  2) Sort By Genres         : :  "<<endl;
          gotoXY(100,12);   cout<< endl << "                                                             : :                            : :  "<<endl;
          gotoXY(100,13); cout<< endl << "                                                             : :                            : :  "<<endl;
          gotoXY(100,14);   cout<< endl << "                                                             : :  3) Favourites List        : :  "<<endl;
          gotoXY(100,15);   cout<< endl << "                                                             : :                            : :  "<<endl;
          gotoXY(100,16); cout<< endl << "                                                             : :............................: :   "<<endl;
          gotoXY(100,17);   cout<< endl << "                                                             : :  4) Exit Program           : :  "<<endl;
          gotoXY(100,18);   cout<< endl << "                                                             : :............................: : "<<endl;
          gotoXY(100,19); cout<< endl << "                                                             :................................: "<<endl;  
          a=3;
          menu_item=3;
          continue;
        }
        if(a==3)
        {
          a=-1;
          continue;
        }
      }

1 个答案:

答案 0 :(得分:0)

在成员函数Song_Name中,您要检查条件两次。尝试这样。

void Song_Name()
{   
    do
    {
        cin << song;

        if (song >= 1 && song <= 18) break;
        else
        {
            // pause and clear
        }

    } while (true);
}

while (running)下的代码重复太多,请尝试

void printMessage()
{
    /*
    gotoXY(75,4);     cout << "Main Menu";
    gotoXY(100,5);  cout << endl << ".................................. " << endl;
    etc
    */
}

while (running)
{
    switch (a) {
    case -1: 
        printMessage();
        a = 0;
        menu_item = 0;
        break;
    case 0:
        printMessage();
        a = 1;
        menu_item = 1;
        break;
    }
}