分配是编写一个程序,允许用户计算各种形状的面积和体积。不允许使用PI
的一个例外的任何全局变量。
#include <iostream>
#include <iomanip>
using namespace std;
//Functions
void showMenu(int &);
double area (double, double);
double area (double);
double volume (double, double, double);
double volume (double);
int main()
{
int choice;
double area, volume;
const double PI = 3.14;
do
{
showMenu();
cin >> choice;
if (choice < 1 || choice > 5 )
{
cout << "Please select a valid choice of 1-5: " << endl;
cin >> choice;
}
else if (choice == 1)
{
area = double area (double length, double width);
cout << "The area of the rectangle is: " << endl;
}
else if (choice == 2)
{
area = double area (double radius);
cout << "The area of the circle is: " << endl;
}
else if (choice == 3)
{
volume = double volume (double length, double width, double height);
cout << "The volume for a box is: " << endl;
}
else if (choice == 4)
{
volume = double volume (double radius);
cout << "The volume of a sphere is: " << endl;
}
}
while (choice != 5);
return 0;
}
void ShowMenu(int &choice)
{
cout << "1. Calculate the area of a rectangle";
cout << "2. Calculate the area of a circle";
cout << "3. Calculate the volume for a box";
cout << "4. Calculate the volume of a sphere";
cout << "5. Quit";
}
double area (double length, double width);
{
cout << "Enter the length: ";
cin >> length;
cout << "Enter the width: ";
cin >> width;
area = lenght * width;
}
double area (double radius);
{
cout << "Enter the radius: ";
cin >> radius;
area = PI * (radius * 2);
}
double volume (double length, double width, double height);
{
cout << "Enter the length: ";
cin >> length;
cout << "Enter the width: ";
cin >> width;
cout << "Enter the height: ";
cin >> height;
volume = length * width * height;
}
double volume (double radius);
{
cout << "Enter the radius: ";
cin >> radius;
volume = (4/3) * PI * (radius * 3)
}
我得到的错误:
1&gt; c:\ users \ dylan \ documents \ visual studio 2010 \ projects \ lab4 \ lab4 \ labfour.cpp(20):错误C2660:'showMenu':函数不带0参数
1&gt; c:\ users \ dylan \ documents \ visual studio 2010 \ projects \ lab4 \ lab4 \ labfour.cpp(31):错误C2062:输入'double'意外
1&gt; c:\ users \ dylan \ documents \ visual studio 2010 \ projects \ lab4 \ lab4 \ labfour.cpp(38):错误C2062:输入'double'意外
1&gt; c:\ users \ dylan \ documents \ visual studio 2010 \ projects \ lab4 \ lab4 \ labfour.cpp(45):错误C2062:输入'double'意外
1&gt; c:\ users \ dylan \ documents \ visual studio 2010 \ projects \ lab4 \ lab4 \ labfour.cpp(52):错误C2062:输入'double'意外
1&gt; c:\ users \ dylan \ documents \ visual studio 2010 \ projects \ lab4 \ lab4 \ labfour.cpp(71):错误C2447:'{':缺少函数头(旧式正式列表?)< / p>
1&gt; c:\ users \ dylan \ documents \ visual studio 2010 \ projects \ lab4 \ lab4 \ labfour.cpp(80):错误C2447:'{':缺少函数头(旧式正式列表?)< / p>
1&gt; c:\ users \ dylan \ documents \ visual studio 2010 \ projects \ lab4 \ lab4 \ labfour.cpp(87):错误C2447:'{':缺少函数头(旧式正式列表?)< / p>
1&gt; c:\ users \ dylan \ documents \ visual studio 2010 \ projects \ lab4 \ lab4 \ labfour.cpp(98):错误C2447:'{':缺少函数头(旧式正式列表?)< / p>
1&GT; Lab4.cpp 1&gt; c:\ users \ dylan \ documents \ visual studio 2010 \ projects \ lab4 \ lab4 \ lab4.cpp(117):警告C4244:'=':从'double'转换为'float',可能会丢失数据< / p>
1&gt; c:\ users \ dylan \ documents \ visual studio 2010 \ projects \ lab4 \ lab4 \ lab4.cpp(127):错误C2447:'{':缺少函数头(旧式正式列表?)< / p>
答案 0 :(得分:5)
纠正编译时错误不是一项艰巨的任务。关注错误消息并尝试找出问题所在。我将在您的代码中向您展示一些解决错误的示例。您的代码有很多错误,表明您需要在C ++中练习更多。
showMenu
的声明是void showMenu(int &)
,但您是通过showMenu()
致电:
void showMenu(int &);
^^^^^
Remove it
此外,要调用函数,您不能传递类型:
area = double area (double length, double width);
^^^^^^ ^^^^^^ ^^^^^^
更多,在实现函数时,不应在函数签名后放置;
:
double area (double radius); <--- remove semicolon
{
答案 1 :(得分:2)
这是错误的:
area = double area (double length, double width);
您将从area()
返回值,而不是声明它。只需调用函数:
area = area(double length, double width);
area(double radius)
中的计算不正确:
area = PI * (radius * 2);
那是计算周长。你应该计算面积:
area = PI * (radius * radius);
我没看到在该函数中如何识别PI
。它在main()
中初始化,但未传递给函数或放在全局范围内。
答案 2 :(得分:1)
你忘记了分号:
volume = (4/3) * PI * (radius * 3)
函数声明错误:
double area (double radius);
{
应该(没有分号)
double area (double radius) {