-6

課題は、ユーザーがさまざまな形状の面積と体積を計算できるようにするプログラムを作成することです。の 1 つの例外を除いて、グローバル変数の使用は許可されていません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(31): エラー C2062: タイプ 'double' 予期しない

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(45): エラー C2062: タイプ 'double' 予期しない

1>c:\users\dylan\documents\visual studio 2010\projects\lab4\lab4\labfour.cpp(52): エラー C2062: タイプ 'double' 予期しない

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(80): エラー C2447: '{' : 関数ヘッダーがありません (古いスタイルの正式なリスト?)

1>c:\users\dylan\documents\visual studio 2010\projects\lab4\lab4\labfour.cpp(87): エラー C2447: '{' : 関数ヘッダーがありません (古いスタイルの正式なリスト?)

1>c:\users\dylan\documents\visual studio 2010\projects\lab4\lab4\labfour.cpp(98): エラー C2447: '{' : 関数ヘッダーがありません (古いスタイルの正式なリスト?)

1> Lab4.cpp 1>c:\users\dylan\documents\visual studio 2010\projects\lab4\lab4\lab4.cpp(117): 警告 C4244: '=': 'double' から 'float' への変換,データ損失の可能性

1>c:\users\dylan\documents\visual studio 2010\projects\lab4\lab4\lab4.cpp(127): エラー C2447: '{' : 関数ヘッダーがありません (古いスタイルの正式なリスト?)

4

3 に答える 3

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
{
于 2013-10-02T07:20:01.997 に答える
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()、関数に渡されたり、グローバル スコープに入れられたりすることはありません。

于 2013-10-02T07:24:11.957 に答える
1

次の後にセミコロンを忘れました:

volume = (4/3) * PI * (radius * 3)

関数宣言が間違っています:

double area (double radius);
{

する必要があります (セミコロンなし)

double area (double radius) {
于 2013-10-02T07:22:19.443 に答える