2013-03-02 39 views
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. 
    } 
} 
+0

标题不太具描述性。 – chris 2013-03-02 04:31:07

+4

定义“不工作”。我敢打赌,它正在发挥作用,并且确实遵循了那里的代码告诉它要做的事情。 – 2013-03-02 04:31:51

+0

你是不是指'while(userChoice!= 6)'? – 2013-03-02 04:32:28

回答

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);              
}                    
+0

哇!我忘了所有关于do-while循环!这是一种拯救生命! :) 谢谢! – booky99 2013-03-02 04:49:24

+1

你也忘了switch语句:-P @ booky99 – 2013-03-02 04:57:25

+0

我同意Aniket。在这种事情上切换语句将使您的工作更容易。 – user1066524 2013-03-02 05:00:47

2

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

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

+1这必须是解决OP问题的最小改动(代码有其他问题,但这一行将解决我认为他在这个问题中提出的问题)。 – WhozCraig 2013-03-02 04:48:51

0

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

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

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; 
     } 
    } 
}