涉及函数& if / else if语句 [英] Class involving functions & if/else if statements

查看:127
本文介绍了涉及函数& if / else if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该任务是编写一个程序,允许用户计算各种形状的面积和体积。不允许对 PI 使用任何全局变量。

The assignment is to write a program that will allow the user to calculate the area and volume of various shapes. Not allowed to use any global variables with the one exception for 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> c:\users\dylan\documents\visual studio 2010 \projects\lab4\lab4\labfour.cpp(20):错误C2660:'showMenu':函数不接受0个参数

1>c:\users\dylan\documents\visual studio 2010\projects\lab4\lab4\labfour.cpp(20): error C2660: 'showMenu' : function does not take 0 arguments

1> c: \users\dylan\documents\visual studio 2010\projects\lab4\lab4\labfour.cpp(31):错误C2062:输入double意外

1>c:\users\dylan\documents\visual studio 2010\projects\lab4\lab4\labfour.cpp(31): error C2062: type 'double' unexpected

1> c:\users\dylan\documents\visual studio 2010 \projects\lab4\lab4\labfour.cpp(38):错误C2062:类型'double'意外

1>c:\users\dylan\documents\visual studio 2010\projects\lab4\lab4\labfour.cpp(38): error C2062: type 'double' unexpected

1> c:\users\dylan\documents\visual studio 2010\projects\lab4\lab4\labfour.cpp(45):错误C2062:输入double意外

1>c:\users\dylan\documents\visual studio 2010\projects\lab4\lab4\labfour.cpp(45): error C2062: type 'double' unexpected

1> c:\users\dylan\documents\visual studio 2010\projects\lab4\lab4 \labfour.cpp(52):error C2062:type'double'unexpected

1>c:\users\dylan\documents\visual studio 2010\projects\lab4\lab4\labfour.cpp(52): error C2062: type 'double' unexpected

1> c:\users\dylan\documents\visual studio 2010 \projects\lab4\lab4\labfour.cpp(71):错误C2447:'{':缺少函数头(旧式正式列表?)

1>c:\users\dylan\documents\visual studio 2010\projects\lab4\lab4\labfour.cpp(71): error C2447: '{' : missing function header (old-style formal list?)

1> c:\users\dylan\documents\visual studio 2010 \projects\lab4\lab4\labfour.cpp(80):错误C2447:'{':缺少函数头(old-样式正式列表?)

1>c:\users\dylan\documents\visual studio 2010\projects\lab4\lab4\labfour.cpp(80): error C2447: '{' : missing function header (old-style formal list?)

1> c:\users\dylan\documents\visual studio 2010\projects\lab4\lab4\labfour .cpp(87):error C2447:'{':missing function header(old-style formal list?)

1>c:\users\dylan\documents\visual studio 2010\projects\lab4\lab4\labfour.cpp(87): error C2447: '{' : missing function header (old-style formal list?)

1> c:\users\dylan\\ \\ documents \visual studio 2010 \projects\lab4\lab4\labfour.cpp(98):错误C2447:'{':缺少函数头(旧式正式列表?)

1>c:\users\dylan\documents\visual studio 2010\projects\lab4\lab4\labfour.cpp(98): error C2447: '{' : missing function header (old-style formal list?)

1> Lab4.cpp
1> c:\users\dylan\documents\visual studio 2010\projects\lab4\lab4\lab4.cpp 117):warning C4244:'=':从'double'转换为'float',可能会丢失数据

1> Lab4.cpp 1>c:\users\dylan\documents\visual studio 2010\projects\lab4\lab4\lab4.cpp(117): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data

1> c:\users\dylan\\ \\ documents \visual studio 2010 \projects\lab4\lab4\lab4.cpp(127):错误C2447:'{':缺少函数头(旧式正式列表?)

1>c:\users\dylan\documents\visual studio 2010\projects\lab4\lab4\lab4.cpp(127): error C2447: '{' : missing function header (old-style formal list?)


推荐答案

更正编译时错误不是一项艰巨的任务。按照错误消息,尝试找出问题。我会给你一些例子在你的代码来解决错误。您的代码有许多错误,显示您需要在C ++中练习更多。

Correcting compile time errors is not a hard task. Follow error messages and try to find out the problem. I will show you some examples in your code to solve errors. Your code has many error which shows you need practice more in C++.

showMenu 的声明是 void showMenu(int&)但是你是通过 showMenu()
$ b

The declaration of showMenu is void showMenu(int &) but you are calling it by showMenu():

void showMenu(int &);
              ^^^^^
                Remove it

您不应传递类型:

area = double area (double length, double width);
       ^^^^^^              ^^^^^^         ^^^^^^

更多,当实现一个函数时,你不应该在函数签名之后放置;

More, when implementing a function, you shall not put ; after function signature:

double area (double radius); <--- remove semicolon
{

这篇关于涉及函数& if / else if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆