使用do while循环的c ++重载区域函数

时间:2018-11-11 21:09:28

标签: c++

输入

R r
8 0
0 9.9
5 -1
3.8 7.2

它给了我这些错误

main.cpp: In function ‘int main()’:
   main.cpp:31:26: warning: ‘radius’ may be used uninitialized in this function [-Wmaybe-uninitialized]
             } while (radius <= 1);
                      ~~~~~~~^~~~
   main.cpp:40:24: warning: ‘length’ may be used uninitialized in this function [-Wmaybe-uninitialized]
             while (length <= 1 && width <= 1);
                    ~~~~~~~^~~~
   main.cpp:40:38: warning: ‘width’ may be used uninitialized in this function [-Wmaybe-uninitialized]
             while (length <= 1 && width <= 1);
                                   ~~~~~~^~~~
#include<iostream>
#include <iomanip>
using namespace std;

// function prototype
double calculateArea(double);
double calculateArea(double, double);

int main()
{
    // variables
    char shape;
    double radius, length, width, area;

    do
    {
        cin >> shape;
    }
    while ( shape != 'c' && shape != 'r');

    if (shape == 'c')
    {
        do
        {
            //input value for radius and validate
        } 
        while (radius <= 1);

        // call calculate area with radius
        area =(calculateArea(radius));
        //output area
        cout << "Area of a circle with radius " << radius << " is " << area << endl;
    }
    else // calculate the area of a rectangle
    { 
        // input values for length and width and validate them
        while (length <= 1 && width <= 1); 
        // call calculate area with length and width
        area = calculateArea(length, width);
        // output area
        cout << "Area of a rectangle with length " << length << " and width " << width << " is " << area << endl;
    }
    return 0;
}

// functions 
double calculateArea(double radius)
{
    double area;
    area = 3.14159 * radius * radius;
    return area;
}

double calculateArea( double length, double width)
{
    double area;
    area = length * width;
    return area;
}

1 个答案:

答案 0 :(得分:0)

该错误消息很明确:半径(在分支上显示)和长度宽度可以未初始化(在其他分支上)使用。

检查您要从用户输入中解析什么。