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

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

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

于 2013-03-02T04:58:30.457 回答
0

使用 switch 将是更好的选择。

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 回答