【问题标题】:Confused about why this main isn't working对为什么这个 main 不起作用感到困惑
【发布时间】:2013-03-02 17:59:33
【问题描述】:

好的,所以我正在尝试编写一个主程序,它将要求用户输入数字 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.
    }
}

【问题讨论】:

  • 标题描述性不太强。
  • 定义“不工作”。我敢打赌它正在工作并且完全按照那里的代码告诉它去做。
  • 这个块似乎没问题。此外,switch-case 更适合您的需求。
  • 如果你指的是这个,你会在你的 tail-else 块和紧随其后的 cin &gt;&gt; userChoice; 中双重提示错误的输入值。
  • @chris - 这是煤气总管! - 他还没有付账!

标签: c++


【解决方案1】:

在 else 块的末尾添加“继续”。

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

【讨论】:

  • +1 这必须是解决 OP 问题的最小更改(代码还有其他问题,但这一行将解决我认为他在这个问题中提出的问题)。
【解决方案2】:

我认为以下程序是您想要的:

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

【讨论】:

  • 哇!!我忘记了所有关于do-while循环的事情!这是一个救生员! :) 谢谢!
  • 你也忘记了 switch 语句:-P @booky99
  • 我同意 Aniket。在这种事情上的 switch 语句会让你的工作更容易。
【解决方案3】:

请参阅this questionthis question 的答案。您需要在每个 cin

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

【讨论】:

    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-29
      • 2011-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多