【发布时间】: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