【问题标题】:How do I make a function call with switch statements after the user chooses their option?用户选择选项后,如何使用 switch 语句进行函数调用?
【发布时间】:2019-10-19 03:38:33
【问题描述】:

我试图在这里进行函数调用,但我不知道出了什么问题。我收到一个错误:

使用了未初始化的局部变量“区域”

我们需要使用switch 案例。如果有人能帮助解决这个问题,那就太好了。

// Include Section
#include <iostream>
#include <cstdlib>
using namespace std;

//Function prototype
double rectangle(double, double);
double triangle(double, double);
double circle(double);

// Main Program
int main()
{
    //Variable declaration
    int choice;
    const double PI = 3.14159; //pi
    double area;
    double length, //length for rectangle
            width; //width for rectangle
    double height; //height for triangle
    double base; //base for triangle
    double radius; //radius for circle

    //Give choices
    cout << "Welcome to Geometry Calculator \n";
    cout << "Pick one option from the following: \n";
    cout << "1. Calculate the Area of a Rectangle \n";
    cout << "2. Calculate the Area of a Triangle \n";
    cout << "3. Calculate the Area of a Circle \n";
    cout << "4. Quit \n\n";

    //input choice
    cout << "Enter your choice (1-4): ";
    cin >> choice;

    //Input Validation
    while (choice <= 0 || choice > 4)
    {
        cout << "Please enter a valid menu chice: ";
        cin >> choice;
    }

    //Switch statement
    switch (choice)
    {
        case 1: //rectangle
            cout << "Enter the LENGTH of your Rectangle: ";
            cin >> length;
            cout << "Enter the WIDTH of your Rectangle: ";
            cin >> width;
            rectangle(length, width);
            //function
            break;

        case 2: //Triangle
            cout << "Enter the LENGTH of the base of your Triangle: ";
            cin >> base;
            cout << "Enter the HEIGHT of your Triangle: ";
            cin >> height;
            triangle(base, height);
            return 0;
            break;

        case 3: //Circle
            cout << "Enter the RADIUS of your Circle: ";
            cin >> radius;
            circle(radius);
            return 0;
            //calculation
            break;

        case 4: //quit
            cout << "You chose to quit. Thanks for using my program! \n\n";
    }
    return 0;
}

//function call
void triangle(double base, double height);
{
    area = base * height * 0.5;
    cout << "The AREA of your Triangle is " << area << ". \n\n";
}

void circle(double radius);
{
    area = PI * radius * radius;
    cout << "The AREA of your Circle is " << area << ". \n\n";
}

double fc = rectangle(double length, double width);
{
    area = length * width;
    cout << "The area of the rectangle is " << area << endl;
}

【问题讨论】:

    标签: c++ function geometry switch-statement


    【解决方案1】:

    这里有多个问题:


    在函数定义中,结尾的分号不应存在:

    void triangle(double base, double height);  // <-- remove this semicolon
    {
        area = base * height * 0.5;
        cout << "The AREA of your Triangle is " << area << ". \n\n";
    }
    

    此错误也出现在其他定义中。分号终止函数前向声明(在文件顶部),但在定义函数时,大括号会替换它。


    您还声明了三个函数triangle/circle/rectangle 以返回double,但在实现中您将它们定义为返回不匹配的void。这可能是您遇到的特定错误的来源。

    更改前向声明以匹配定义,反之亦然。


    这三个函数还尝试分配给不在作用域内的area 变量。如果要在这些函数中使用该变量,则必须在这些函数中声明它。

    请注意,这些函数无法访问 main() 中的 area 变量。


    rectangle 的定义以变量声明开头,这没有任何意义:

    double fc = rectangle(double length, double width);
    

    这被解析为全局 fc 变量的声明,该变量被初始化为调用 rectangle 函数的结果,但参数语法不正确,因此解析将失败。大概应该这样写:

    void rectangle(double length, double width)
    

    double rectangle(double length, double width)
    

    函数circle() 引用符号PI,但这不在范围内。 (您在 main() 内声明 PI,因此它仅在该函数中可见。您可以通过将其移至全局范围来解决此问题。)

    【讨论】:

    • 我尝试删除分号,但仍然遇到相同的错误。如果有帮助,我已经添加了错误图片。
    • @qwerty 那是因为不止一个错误;请参阅我的其余答案。
    • 是的,我意识到矩形定义中的错误。您可以使用错误图像查看任何其他错误吗?
    • 能否请您解释一下 void 的用途?
    • @qwerty 用作返回类型时,void表示根本没有返回值; void 函数不返回任何值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多