【问题标题】:C++ How to get the menu options to return to top if choice is < 5如果选择<5,C ++如何让菜单选项返回顶部
【发布时间】:2015-02-10 07:54:47
【问题描述】:

我在第一个菜单中使用了 for 循环,但我决定添加一个二级菜单选项以消除混乱。

当我添加第二个菜单时,for 循环搞砸了一切,所以我将其删除。我只是希望程序能够在用户输入菜单列表中不存在的数字时说“数字无效,请输入正确的数字”。

代码如下...

#include <iostream>
#include <cmath>

using namespace std;

int main ()
{

    double mi, ki, F, C;
    double lbs, kg, ft, m;
    int choice, choice2 = 0;


    cout<<"              MENU ONE               " << endl;
    cout<<"*************************************" << endl;
    cout<<"|1.) Convert miles to kilometers    |" << endl;
    cout<<"|2.) Convert kilometers to miles    |" << endl;
    cout<<"|3.) Convert Fahrenheit to Celsius  |" << endl;
    cout<<"|4.) Convert Celsius to Fahrenheit  |" << endl;
    cout<<"|5.)      FOR MORE CONVERSIONS      |" << endl;
    cout<<"*************************************" << endl;
    cin>> choice;

    if (choice >= 1 && choice < 5){
    switch (choice)
    {
        case 1:
            cout<< "Now enter the amount of miles you wish to convert: " <<endl;
            cin>> mi;
            cout<< "The answer is: " << mi / 0.62137 <<" Kilometers" <<endl;
            break;

        case 2:
            cout<<"Now enter the amount of Kilometers you want to convert: " << endl;
            cin>> ki;
            cout<< "The answer is: " << ki * .62137 <<" Miles " <<endl;
            break;

        case 3:
            cout<<"Now enter the degree in Fahrenheit: " << endl;
            cin>>F;
            cout<< "The answer is: " << (F-32)/1.8 <<" degrees Celsius "<< endl;
            break;

        case 4:
            cout<<"Now enter the degree in Celsius: " << endl;
            cin>>C;
            cout<< "The answer is: " << (C*1.8)+32 <<" degress Fahrenheit "  << endl;
            break;


        }
    }

//********************************************************************************************************//

    else if (choice == 5)
    {
        cout<<"             MENU TWO            " << endl;
        cout<<"*********************************" << endl;
        cout<<"|1.) Convert Pounds to Kilograms|" << endl;
        cout<<"|2.) Convert Kilograms to Pounds|" << endl;
        cout<<"|3.) Convert Feet to Meters     |" << endl;
        cout<<"|4.) Convert Meters to Feet     |" << endl;
        cout<<"|********************************" << endl;
        cin>>choice2;
    }
        if (choice2 >= 1 && choice2 < 5){
        switch (choice2)
        {


        case 1:
            cout<<"Now enter the weight in pounds: " <<endl;
            cin>>lbs;
            cout<<"The answer is: " << lbs / 2.2046 <<" kilograms " << endl;
            break;

        case 2:
            cout<<"Now enter the weight in kilograms: " << endl;
            cin>>kg;
            cout<< "The answer is: " << kg * 2.2046 <<" pounds "<< endl;
            break;

        case 3:
            cout<<"Now enter the number of feet to be converted to 'M': " <<endl;
            cin>>ft;
            cout<<"The answer is: " << (ft / 3.20808) << " meters "<< endl;
            break;

        case 4:
            cout<<"Now enter the number of meters to be converted to 'FT': " <<endl;
            cin>>m;
            cout<<"The answer is: " << (m * 3.20808) << " feet "<< endl;
            break;

            return 0;

            }

        }


    return 0;
}

【问题讨论】:

    标签: c++


    【解决方案1】:

    为两个开关盒添加默认值:

    default:
      cout << "Invalid input..." ;
      break ;
    

    【讨论】:

      【解决方案2】:

      您可能希望将switch (choice2) 放在else if 块中(因为choice2 是该块中的局部变量 - 您无法在该语句块之外访问它;它也会遵循案例 1 到 4,这将表示第二个菜单在未被要求时显示)。然后,您可以在 ifelse if 语句之后使用 else 块打印出错误消息。

      if (choice >=1 && choice < 5)
      {
          switch (choice)
          // the switch
      }
      else if (choice == 5)
      {
          // the second menu
          switch (choice2)
          {
              // switch...
          }
      else {
          // print error message
      }
      

      或者您可以将所有内容都放在一个 switch 中。

      然后,将所有内容放回一个循环中(或使用 goto),并在选择 == 5 的情况下添加一个类似的循环(如果您想确保用户在第二个菜单上输入正确的数字)。对循环的条件使用布尔值;当用户输入错误时将其设置为 true,否则设置为 false。

      bool repeat = false;
      while (repeat)
      {
          // ask the user to input a number
          switch (choice)
          {
          // cases 1 through 4 (omitted)
          // don't forget to set repeat to false for these cases
          case 5:
          // print second menu and input choice2
              switch (choice2)
              {
                  // prompt the user to enter another number
                  bool repeat2 = false;
                  while (repeat2)
                  {
                  // cases 1 through 4
                  default: // use the default case label for numbers that are not 1 to 4
                  // print error message
                  repeat2 = true;
                  }
              }
              repeat = false;
          break;
          default: // use the default case label for numbers that are not 1 to 5
              // print error message
              repeat = true;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-02
        • 1970-01-01
        • 2011-02-18
        • 2016-12-06
        相关资源
        最近更新 更多