0

わかりましたので、ユーザーに 1 から 6 までの数字を入力するように要求し、数字が 6 の場合はプログラムを終了するメインを作成しようとしています。6 より大きい場合は、番号の再入力を求められます。問題は、実行すると「if」ステートメントがチェックされず、「別のオプションを入力してください」という行に自動的に移動することです。

私のプログラムがこのようなことをする理由は何ですか?

更新: すべてのステートメントを自動的にスキップし、ループif内の最後の質問を行うと言っています。while

int main()
{
    int userChoice = 0;

    print(); //printing all of the options.

    cout << "Please enter one of the options listed below" <<endl;
    cin >> userChoice;

    while(userChoice != 6)// 6 = the user wishing the end the program when they press 6.
    {
        if(userChoice == 1) //adding integer to the front of the list
        {
            addValueFront();
        }
        else if(userChoice == 2)//adding integer to the back of the list
        {
            addValueBack();
        }
        else if(userChoice == 3)//removing from the list
        {
            int n = 0;
            cout << "Please enter the integer you wish to remove" << endl;
            cin >> n;
            removeValue(n);
        }
        else if(userChoice == 4)//printing the list
        {
            printList();
        }
        else if(userChoice == 5)//printing the number of items from the list
        {
            printItem();
        }

        else
        {
            cout << "The number you have entered is too high. Please try again" << endl;
            cin >> userChoice;
        }
        cout << "please enter another option" <<endl;

        cin >> userChoice; //sets up which option the user can choose from.
    }
}
4

4 に答える 4

2

else ブロックの最後に「継続」を追加します。

cout << "The number you have entered is too high. Please try again" << endl;
cin >> userChoice;
continue;
于 2013-03-02T04:42:16.680 に答える
1

次のプログラムが必要だと思います。

int main()                                                                     
{                                                                              
  int userChoice = 0;                                                         

  print(); //printing all of the options.                                      

  cout << "Please enter one of the options listed below" <<endl;                                                                                                  
  do // 6 = the user wishing the end the program when they press 6.                  
  {                                                                            
    cin >> userChoice;
    if(userChoice > 6)                                                      
    {                                                                       
      cout << "The number you have entered is too high. Please try again" << endl;
      cout << "please enter another option" <<endl;                         
    }                                                                       
    else if(userChoice == 1) //adding integer to the front of the list         
    {                                                                          
      addValueFront();                                                         
    }                                                                          
    else if(userChoice == 2)//adding integer to the back of the list           
    {
      addValueBack();                                                          
    }
    else if(userChoice == 3)//removing from the list                           
    {
      int n = 0;                                                               
      cout << "Please enter the integer you wish to remove" << endl;           
      cin >> n;                                                                
      removeValue(n);                                                          
    }                                                                          
    else if(userChoice == 4)//printing the list                                
    {                                                                          
      printList();                                                             
    }                                                                          
    else if(userChoice == 5)//printing the number of items from the list       
    {                                                                          
      printItem();                                                             
    }                                                                          
  } while(userChoice != 6);                                                    
}                                                                            
于 2013-03-02T04:43:31.920 に答える
0

この質問この質問の回答を参照してください。すべての cin<< の前に cin.clear() および cin.ignore() を呼び出して、キーボード バッファーをフラッシュし、適切かつ一貫して動作するようにする必要があります。

さらに、論理的に正しくないため、else ブロックから cin を削除する必要があります。

于 2013-03-02T04:58:30.457 に答える
0

スイッチを使用すると、より良いオプションになります。

int main()
{
    int userChoice = 0;

    print(); //printing all of the options.

    cout << "Please enter one of the options listed below" <<endl;
    cin >> userChoice;

    while(1)// 6 = the user wishing the end the program when they press 6.
    {
        if(userChoice == 1) //adding integer to the front of the list
        {
            addValueFront();
        }
        else if(userChoice == 2)//adding integer to the back of the list
        {
            addValueBack();
        }
        else if(userChoice == 3)//removing from the list
        {
            int n = 0;
            cout << "Please enter the integer you wish to remove" << endl;
            cin >> n;
            removeValue(n);
        }
        else if(userChoice == 4)//printing the list
        {
            printList();
        }
        else if(userChoice == 5)//printing the number of items from the list
        {
            printItem();
        }
        else if(userChoice == 6)// 6 = the user wishing the end the program when they press 6.
        {
            return 0;
        }
        else
        {
            cout << "The number you have entered is too high. Please try again" << endl;
            cin >> userChoice;
        }
    }
}
于 2013-03-02T09:59:33.500 に答える