对为什么这个主要不起作用感到困惑

时间:2013-03-02 04:30:15

标签: c++

好的,所以我正在尝试编写一个主要位置,它会要求用户输入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 个答案:

答案 0 :(得分:2)

在else块的末尾添加“continue”。

cout << "The number you have entered is too high. Please try again" << endl;
cin >> userChoice;
continue;

答案 1 :(得分: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);                                                    
}                                                                            

答案 2 :(得分:0)

请参阅this questionthis question的答案。你需要在每个cin&lt;&lt;之前调用cin.clear()和cin.ignore()来刷新键盘缓冲区,使其行为正常且一致。

此外,你应该从else块中删除cin,因为它在逻辑上是不正确的。

答案 3 :(得分: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;
        }
    }
}
相关问题