2013-10-02 64 views
-6

該任務是編寫一個程序,該程序將允許用戶計算各種形狀的面積和體積。除PI之外,不允許使用任何全局變量。涉及功能和if/else if語句的類

#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:\用戶\迪倫\文檔\ Visual Studio 2010的\項目\ lab4 \ lab4 \ labfour.cpp(20):錯誤C2660 :'showMenu':函數不帶0參數

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

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

1> c:\ users \ dylan \ documents \ visual studio 2010 \ projects \ lab4 \ lab4 \ labfour.cpp(45):error C2062:type'雙」意外

1> C:\用戶\迪倫\文件\視覺工作室2010 \項目\ lab4 \ lab4 \ labfour.cpp(52):錯誤C2062:類型 '雙' 意外

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):error 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):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):警告C4244:'=':從'double'轉換爲'float',可能丟失數據

1> c:\ users \ dylan \ documents \ visual studio 2010 \ projects \ lab4 \ lab4 \ lab4.cpp(127):error C2447:'{':缺少函數標題(舊式正式列表?)

+2

你應該去上班時間。 – Adam

+3

這些錯誤非常明顯,只需檢查錯誤的指定行號上的語句即可。 – HAL

回答

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 
{ 
+0

傳入'showMenu'的值根本不影響函數;)當然這不會影響你答案的有效性,但是你仍然可以從函數簽名 – nijansen

+0

中刪除'int'我懷疑最初的意圖那麼showMenu()實際上也應該獲得選擇,所以cin >>選擇;應該已經在showMenu()函數中。 – djna

+0

感謝您的回覆。解決大部分錯誤,但仍然有一些。 – user2837593

1

你忘了一個分號後:

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

函數聲明是錯誤的:

double area (double radius); 
{ 

應該是(沒有分號)

double area (double radius) { 
+0

感謝您的回覆。解決大部分錯誤,但仍然有一些。 – user2837593

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()中初始化,但不傳遞給函數或放在全局範圍內。