【问题标题】:Returning to the menu & exiting the program返回菜单并退出程序
【发布时间】:2016-11-12 22:51:18
【问题描述】:

(C++) 所以我正在为我的作业编写一个程序,它要求程序在每种情况下提示用户返回菜单或退出程序,问题是我不确定如何有效地实现这一点。有任何想法吗?同样为了退出,我想调用最后一个名为'exit'的函数

 #include <iostream>                                                                //libraries


using std::cout;
using std::cin;
using std::endl;

int menu(double pi);
int circleArea(double pi);
int circleCircum(double pi);                                                    //function declarations for the demanded shapes, passing pi in order to reuse it in both circle calculations
int rectanArea();
int triangArea();
int cubVol();
void exit();

int main()
{
    double pi = 3.14159265359;                                                  //declaration of pi which I will be using in the circles (passed)
    menu(pi);
    system("PAUSE");
    return 0;
}

int menu(double pi)                                                             //menu for choosing a shape
{
    int choice = 0;
    cout << "Shape Calculator Created By:\n\nPlease select what you wish to calculate:\n\n1 - Area of a Circle\n\n2 - Circumference of a Circle\n\n3 - Area of a Rectangle\n\n4 - Area of a Triangle\n\n5 - Volume of a Cuboid\n\n ";
    cin >> choice;
    system("CLS");

     switch (choice)                                                                //switch case for each shape
        {
        case 1:
            circleArea(pi);
            break;
        case 2:
            circleCircum(pi);
            break;
        case 3:
            rectanArea();
            break;
        case 4:
            triangArea();
            break;
        case 5:
            cubVol();
            break;
        default:
            cout << "Invalid input! Please try again.\n\n";
            break;
        }
    return 0;
}

int circleArea(double pi)                                                       //the area of circle function
{
    double radius = 0;

    cout << "Please enter the radius of your circle:\n\n";                      //asks for the radius first
    cin >> radius;
    system("CLS");

    cout << "The area of your Circle is:\n\n" << radius*radius*pi << "cm/2\n\n";                                //display the calculation which is calculated using the user input, essentially the formula is PI*radius(squared) just reversed
    return 0;
}


int circleCircum(double pi)                                                     //the circumference of circle function
{
    double radius = 0.0;

    cout << "Please enter the radius of your circle:\n\n";                      //asks for radius first
    cin >> radius;
    system("CLS");

    cout << "The circumference of your Circle is:\n\n" << pi*radius * 2 << "cm\n\n";                            //calculates the circumference using the user input, formula is 2*PI*radius but reversed
    return 0;
}

int rectanArea()                                                                //function for area of rectangle
{
    double rectanHeight = 0.0;
    double rectanWidth = 0.0;

    cout << "Please enter the height of your Rectangle:\n\n";                   //asks for input for the height first
    cin >> rectanHeight;
    system("CLS");

    cout << "Height = " << rectanHeight << endl << endl << "Please enter the width of your Rectangle\n\n";      //shows the inputted height above, then asks for input of width
    cin >> rectanWidth;
    system("CLS");

    cout << "The area of your Rectangle is:\n\n" << rectanHeight*rectanWidth << "cm/2\n\n";                     //calculates the area of rectangle using the input, formula is height*width then displays it
    return 0;
}

int triangArea()                                                                //function for area of triangle
{
    double triangBase = 0.0;
    double triangHeight = 0.0;

    cout << "Please enter the base measurement of your Triangle\n\n";           //asks for input of the base first
    cin >> triangBase;  
    system("CLS");

    cout << "Base = " << triangBase << endl << endl << "Please enter the height of your Triangle\n\n";          //displays inputted base and then asks for the height
    cin >> triangHeight;
    system("CLS");

    cout << "The area of your Triangle is:\n\n" << triangBase*triangHeight / 2 << "cm/2\n\n";                   //calculates area using user input and displays it, formula is base*height/2
    return 0;
}

int cubVol()                                                                    //function for volume of a cuboid
{
    double cubLength = 0.0;
    double cubWidth = 0.0;
    double cubHeight = 0.0;

    cout << "Please enter the length of your Cuboid\n\n";                       //asks for input of length first
    cin >> cubLength;
    system("CLS");

    cout << "Length = " << cubLength << endl << endl << "Please enter the width of your Cuboid\n\n";            //displays inputted length and asks for the width
    cin >> cubWidth;
    system("CLS");

    cout << "Length = " << cubLength << endl << "Width = " << cubWidth << endl << endl << "Please enter the height of your Cuboid:\n\n";        //displays inputted length and width, asks for the height finally
    cin >> cubHeight;
    system("CLS");

    cout << "The volume of your Cuboid is:\n\n" << cubLength*cubWidth*cubHeight << "cm/3\n\n";                  //displays the calculation using inputted information, formula is length*width*height
    return 0;

}

void exit()
{
        cout << "**************************************" << endl;
        cout << "**************************************" << endl;
        cout << "*******    T H A N K  Y O U    *******" << endl;
        cout << "*******    F O R  U S I N G    *******" << endl;
        cout << "******* T H I S  P R O G R A M *******" << endl;
        cout << "**************************************" << endl;
        cout << "**************************************" << endl;
}

【问题讨论】:

  • 阅读您最喜欢的 C++ 教科书中的循环。
  • 是的,我考虑过使用 do_while 循环,但我不确定如何使用它们退出并在一个循环中返回菜单。

标签: c++ menu switch-statement exit


【解决方案1】:

在您的主菜单(pi)中,替换为:

char choice;
bool exitNow = false;

do{
    menu(pi);
    cout<<"Return (r) or quit (q)?"<<endl;
    cin>>choice;
    if(choice == 'q')
        exitNow = true;
} while (!exitNow);
exit();

即使我没有测试它也应该可以工作。

编辑: 上面的代码不检查是否输入了正确的输入。这应该可以解决:

char choice = 0;
bool exitNow = false;

do{
    menu(pi);

    do{
        cout<<"Return (r) or quit (q)?"<<endl;
        cin>>choice;
    }
    while( !(choice == 'r' || choice == 'q') && cout<<"Invalid input!"<<endl);

    if(choice == 'q')
        exitNow = true;
} while (!exitNow);
exit();

【讨论】:

  • 嗯,我试过了,虽然在输入 r 或 q 后它只显示“无效输入”。你介意解释一下这段代码吗?我实际上是在 9 月才开始编程的:P
  • 对不起,我的错!已编辑和更正。我使用 do-while 循环来运行指令至少 1 次。执行 menu(pi) 后,用户会收到一条消息,要求输入“r”或“q”(您也可以使用整数,只需更改类型)。内部 while 条件检查选择是否不正确,如果是,则打印“无效输入!” (仅当第一个条件为真时才执行 cout)。之后,如果选择是 'q',则 exitNow 设置为 true 并结束循环。希望对您有所帮助!
  • 啊,这让它更清楚了,非常感谢。还有一件事,内部支架实际上是做什么的?:(!(choice == 'r' || choice == 'q') &amp;&amp; cout &lt;&lt; "Invalid input!" &lt;&lt; endl)
  • 我必须否定choice == 'r' || choice == 'q',所以它是!(choice == 'r' || choice == 'q')。写!choice == 'r' || choice == 'q'意味着否定唯一选择(这是真的,因为它不同于0)所以我基本上有一个false == 'r' || choice == 'q'
  • 刚刚注意到我可以使用&amp;&amp; 而不是|| 没有否定。choice == 'r' &amp;&amp; choice == 'q' &amp;&amp; cout&lt;&lt;"Invalid input!"&lt;&lt;endl
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多