C++ 输入验证和退出循环,创建函数

C++ 输入验证和退出循环,创建函数,c++,function,loops,C++,Function,Loops,我试图创建一个用于输入验证的循环,如果用户想要退出,则创建另一个循环。还要创建四个函数,一个用于菜单,一个用于三个面积计算 我应该在哪里实现更新的代码?我应该使用新变量还是使用旧变量。谢谢你 以下是我目前的代码: #include <iostream> #include <cmath> using namespace std; int main() { const float PI = 3.14159; int user_choice; /

我试图创建一个用于输入验证的循环,如果用户想要退出,则创建另一个循环。还要创建四个函数,一个用于菜单,一个用于三个面积计算

我应该在哪里实现更新的代码?我应该使用新变量还是使用旧变量。谢谢你

以下是我目前的代码:

#include <iostream>
#include <cmath>    
using namespace std;
int main()
{
    const float PI = 3.14159;
    int user_choice;
    // main screen prompting for choice
    cout << "\nGeometry Calculator\n"
        << "   1. Calculate the Area of a Circle\n"
        << "   2. Calculate the Area of a Rectangle\n"
        << "   3. Calculate the Area of a Triangle\n"
        << "   4. Quit\n"
        << "\nEnter you choice (1-4): ";
    cin >> user_choice;
    cout << endl;

    switch (user_choice)
    {
        float area;
        // if user chooses 1
    case 1:
        int radius;
        // prompt user for radius
        cout << "What is the radius: ";
        cin >> radius;
        // if user enters radius as negative number
        if (radius < 0)
        {
            cout << "\nThe radius must be a positive number.\n"
                << "Try again.\n"
                << endl;
        }
        else
            // calculate area
        {
            area = PI * pow(radius, 2);

            cout << "The area of the circle is  "
                << area << endl;
        }
        break;
        // if user chooses 2
    case 2:
        float width, length;
        // prompt user for height and width
        cout << "What is the length? " << endl;
        cin >> length;

        if (length > 0)
        {
            cout << "What is the width? " << endl;
            cin >> width;
            // calculate and display area
            if (width > 0) {
                area = length * width;
                cout << "The area of rectangle is "
                    << area
                    << endl;
            }
            // if user enters a height or width less than 0
            else {
                cout << "\nWidth must be a positive number"
                    << endl;
                cout << "Try again." << endl;
            }

        }
        else
        {
            cout << "\nLength must be a positive number" << endl;
            cout << "Try again." << endl;
        }
        break;
        // if user chooses 3
    case 3:
        float height,
            base;
        // prompt user for base and height
        cout << "What is the base? ";
        cin >> base;

        if (base > 0)
        {
            cout << "What is the height? ";
            cin >> height;
            // calculate and display area
            if (height > 0)
            {
                area = (base * height) * .5;
                cout << "Area of triangle is "
                    << area
                    << endl;
            }
            // if user inputs a height or base less than 0
            else
            {
                cout << "\nHeight must be a positive number\n"
                    << "Try again."
                    << endl;
            }

        }
        else
        {
            cout << "\nBase length must be a positive number\n"
                << "Try again." << endl;
        }
        break;
        // if user chooses 4
    case 4:
        cout << "You have chose to quit the program. Good-bye." << endl;
        break;
        // if user enters input other than 1-4
    default:
        cout << "\nYour choice must be between 1 and 4.\n"
            << "Try again."
            << endl;
        break;
    }
#包括
#包括
使用名称空间std;
int main()
{
常数浮点PI=3.14159;
int用户选择;
//主屏幕提示选择

cout如果我了解您想要什么,您应该创建一个
while(true)
循环,其中包括用户主选项(主菜单)。我会这样做:


void calculateCircleArea() {
    // Circle area calculation
}

void calculateRectangleArea() {
    // Rectangle area calculation
}

void calculateTriangleArea() {
    // Triangle area calculation
}

int main()
{
    // other main stuff

    int user_choice=0;
    while (user_choice!=4)
    {
        cout << "\nGeometry Calculator\n"
        << "   1. Calculate the Area of a Circle\n"
        << "   2. Calculate the Area of a Rectangle\n"
        << "   3. Calculate the Area of a Triangle\n"
        << "   4. Quit\n"
        << "\nEnter you choice (1-4): ";
        cin >> user_choice;

        if (user_choice<1 || user_choice>4)    // example to inform the user of a bad input
        {
            cout << "Wrong input\n";
            cin.clear();
            cin.ignore(INTMAX_MAX, '\n');
            // Ignoring and clearing all the input until the delimiter character ('\n')
            continue; // it is unnecessary, but this keyword will skip the cycle in which it is to its next iteration
        }
        
        switch(user_choice)
        {
            case 1:
            calculateCircleArea();
            break;
            case 2:
            calculateRectangleArea();
            break;
            case 3:
            calculateTriangleArea();
            break;
            case 4:
            // nothing, because the value of the variable user_choice is 4, so the loop will end
            break;
            default:
            // default stuff
            break;
        }    // switch
    }    // while

}    // main

void calculateCircleArea(){
//圆面积计算
}
空隙计算矩形面积(){
//矩形面积计算
}
无效面积(){
//三角形面积计算
}
int main()
{
//其他主要材料
int user_choice=0;
while(用户选择!=4)
{

cout“我应该在哪里实现更新的代码?我应该使用新的变量还是使用旧的变量?”听起来你在问关于软件设计的问题。如果你有一个工作程序需要一些评论,你可以问进去。我不确定问题出在哪里。你的问题似乎在某种程度上并不重要。我认为你最好的方法是选择其中一个函数并试一试(绝对不要试图一次完成所有任务)。如果您陷入困境或它似乎不正确,请发布您正在努力解决的代码。这样,您的关注点会更清楚,至少目前对我来说还不是很清楚。此外,在编写函数后添加循环可能会更容易。代码应该更易于管理(假设您在函数方面做得很好)。这不会检查
cin>>user\u choice;
的返回值,因此如果用户输入的不是数字的内容将不会被提取,您将陷入一个无限循环的“错误输入”@JerryJeremiah,这是有意的,它将“强制”如果用户只输入一个受支持的选项,程序将一直运行,直到发出正确的停止信号,因此值为4。我看到意外行为的唯一方法是用户插入十进制值介于0和4之间的字符,但这是极不可能的。即使如此,也可以使用
scanf(%d\n,&user\u choice)轻松修复该错误
或显式强制转换:
user\u choice=(int)user\u choice
。不过,我认识到,在输入上添加长度检查是个好主意,但我认为,这与op询问的内容无关,但当我运行它并在提示下输入字母“a”时,它进入了一个无限循环打印“错误输入”,因为字母“a”永远不会被cin提取为整数变量从输入流中删除。你完全正确,我编辑了我的答案来解决这个问题。如果输入错误,我会清除输入。谢谢你指出。