簡體   English   中英

沒有獲得正確的輸入

[英]Cin not getting the proper input

我用C ++編寫的程序有問題。 請注意,我不在筆記本電腦上(接下來的8個小時我都在工作),因此我無法在此處復制代碼,但是我在此問題上花費了幾個小時,所以我知道出了什么問題,請不要不知道如何解決。 所以我的代碼看起來像這樣:

bool valid = false;
int choice;

cout << "Please make a choice between 1 and 4\n"; 
//Here I enumarate 4 choices
cin >> choice;  //first input

//note that I do verify that an integer is entered and that it's between 1 and 4
//my code works so far

valid = true; //if the choise is good

while(valid)
{
     if (choice == 1)
     {
         //here I open a url
         cout << "If you would like to stick with this choice, presse 1 again or make  another choice \n";

         //This is where the code does not work at all
         cin >> choice; //second input

         if (choice == 1) //this line is to get out of the loop but I never reach it
             valid = false;
     }
     if (choice == 2) //contains similar code to the 1st if
     {
     }
     if (choice == 3) //contains similar code to the 1st if
     {
     }
     if (choice == 4) //contains similar code to the 1st if
     {
     }
     else
     {
          cout << "This is not one the choices. Choose again!";
          cin >> choice;
     }
}

當用戶選擇第一個選項時,我只會填寫第一個選項,因為其他三個選項的行為相同,只是鏈接不同。 這是用戶選擇第一個選項時程序的行為。 它會很好地打開鏈接,然后要求用戶再次按1以保留該選項或進行其他選擇。 它總是進入else條件后言。 即使按1退出循環,它也會進入其他循環。 這意味着無論我鍵入什么輸入,都不會將其注冊為1到4之間的整數。

我嘗試調試以查看第二個輸入后“ choice”具有什么值,它顯示49! 自從我鍵入1以來,這沒有任何意義。但是,在其他情況下,如果我在1和4之間做出正確選擇,例如1,該鏈接將再次打開,程序將再次要求我按1進行粘貼根據我的選擇或做出其他選擇。

我認為緩沖區上還有東西。 我閱讀了一些有關它的內容以了解如何清除它,但是我在第一個cin之后嘗試了cin.clear(),cin.ignore(),cin.sync(),但第二個輸入始終與我實際鍵入的內容不同不管我鍵入什么,它都會再次進入其他。 這是從地獄到死的無限循環。

我還是c ++的新手,正在嘗試一些想法。 我嘗試自己解決此問題,但需要幫助。 謝謝。

//Your control block was not correct. 
//Your logic falls into the else block after the second input 
//has been read because it use the new value of choice and run through all the
//if statements. In a case where you have multiple options in that manner use 'else if'` 

void start()
{
    bool valid = false;
    int choice;

    cout << "Please make a choice between 1 and 4\n"; 
    //Here I enumarate 4 choices
    cin >> choice;  //first input

    //note that I do verify that an integer is entered and that it's between 1 and 4
    //my code works so far

    valid = true; //if the choise is good

    while(valid)
    {
         if (choice == 1)
         {
             //here I open a url
             cout << "If you would like to stick with this choice, presse 1 again or make  another       choice \n";

             //This is where the code does not work at all
             cin >> choice; //second input

             if (choice == 1)
             {
                valid = false;
             } //this line is to get out of the loop but I never reach it

         }

         else if (choice == 2) //contains similar code to the 1st if
         {
         }
         else if (choice == 3) //contains similar code to the 1st if
         {
         }
         else if (choice == 4) //contains similar code to the 1st if
         {
         }
         else
         {
              cout << "This is not one the choices. Choose again!";
              cin >> choice;
         }
    }   
}
int main(void)
{
    start();
    return 0;
}

正如編解碼器所指出的那樣, if else您的else語句不正確,則在確認選擇后,它將您的選擇與2、3和4的值進行比較。由於現在為1,它將進入else語句並在其中執行任何操作。 我的建議是使用switch語句,這將更容易閱讀。

cin >> choice;

switch(choice)
{
    case 1:
    // do whatever you want
    break;
    case 2:
    // do whatever you want
    break;
    // continue with your choice
    default:
    // do whatever you want if the value falls out of your choice
    cout << "This is not the right choice" << endl;
    break;
}

在您的while循環中,使用這種方式:

while(valid)
   {
    if(choice==1)
      {
        //your code
      }
else if(choice==2)
      {
       //your code
      }
else if(choice==3)
      {
       //your code
      }
else if(choice==4)
      {
       //your code
      }
else 
      {
       //your code
      }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM