2015-06-24 53 views
0

對於一個項目,我必須創建一個簡單的菜單驅動的轉換軟件包,用戶輸入一個數字並轉換其他東西,米到腳,米到米等。我必須爲每次轉換使用一個函數。我已經開始使用米到腳功能。但是當我編譯它時,我收到錯誤消息「之前不允許使用函數定義'('token')。我該如何解決?我也是一名初學者。爲什麼我不斷收到錯誤消息「在C++中{'token'之前不允許使用函數定義?

#include <iostream> 

using namespace std; 

double meters_infeet(double meters){ 
double totalmetersinfeet = (meters * 3.280839895); 
return totalmetersinfeet; 
} 
double feet_inmeters(double feet){ 
double totalfeetinmeters = (feet/3.280839895); 
return totalfeetinmeters; 
} 
double area_infeet(double length_inmeters, double width_inmeters){ 
double totalareainfeet = (length_inmeters * 3.280839895) 
return totalareainfeet; 
} 
double area_inmeters(double length_infeet, double width_infeet){ 
double totalareainmeters = (length_infeet/3.280839895) 
return totalareainmeters; 
} 



int main() 
{ 
int choice; 
double meters; 
double feet; 
double length_inmeters; 
double width_inmeters; 
double length_infeet; 
double width_infeet; 

do{ 

cout << "English-Metric Junior" << endl; 
cout << "1)Convert from meters to feet" << endl; 
cout << "2) Convert from feet to meters" << endl; 
cout << "3) Compute the area of a rectangle in square feet given width 
cout << "4) Compute the area of a rectangle in meters given the length 
cout << "5) Quit the Program" << endl; 
cout << "Please enter a number (1-5)" << endl; 
cin >> choice; 
}while(choice > 5); 



switch (choice) 
{ 
case 1: 
cout << "Enter Meters" << endl; 
cin >> meters; 
cout << meters_infeet(meters)<< "Feet " << endl; 
break; 
case 2: 
cout << "Enter Feet" << endl; 
cin >> feet; 
cout << feet_inmeters(feet)<< "Meters " << endl; 
break; 
case 3: 
cout << "Enter length in meters" << endl; 
cin >> length_inmeters; 
cout << "Enter width in meters" << endl; 
cin >> width_inmeters; 
cout << area_infeet(length_inmeters, width_inmeters) << endl; 
break; 
case 4: 
cout << "Enter length in feet" << endl; 
cin >> length_infeet; 
cout << "Enter width in feet" << endl; 
cin >> width_infeet; 
cout << area_inmeters(length_infeet, width_infeet)<< endl; 
break; 
+1

您應該添加,你有你的錯誤的行號內。你的錯誤很簡單:注意你的'switch'在主函數之外,你應該將第一個'}'移動到文件的底部。 –

回答

1

事實上,你的功能meters_infeet

double meters_infeet(double meters) 
{ 
    double totalmetersinfeet = meters * 3.280839895; 
    return totalmetersinfeet; 


    int main() 

中定義的功能main你錯過的功能閉括號。 meters_infeet

看來,這個大括號

cin >> choice; 
} 

是必須的meters_infeet return語句後main前右括號。

它丟失了該計劃。:)

而在這個代碼片段

switch (choice) 
{ 
case 1: 
cout << "Enter Meters" << endl; 
cin >> meters; 
double meters_infeet(double meters); 
break; 
} 

聲明

double meters_infeet(double meters); 

是一個函數聲明。 我想你的意思是這樣

double feets = meters_infeet(meters); 

編輯:這是一個壞主意,更改代碼中的問題時,前原題更新已經回答了。因爲在這種情況下,讀者不會看到問題與答案之間的關係。

所有這些功能首先是沒有意義

double area_infeet(double length_inmeters, double width_inmeters){ 
double totalareainfeet = (length_inmeters * 3.280839895) 
return totalareainfeet; 
} 
double area_inmeters(double length_infeet, double width_infeet){ 
double totalareainmeters = (length_infeet/3.280839895) 
return totalareainmeters; 
} 

他們的第二個參數不使用。

switch語句應該是do-while語句

do { 
cout << "English-Metric Junior" << endl; 
cout << "1)Convert from meters to feet" << endl; 
cout << "2) Convert from feet to meters" << endl; 
cout << "3) Compute the area of a rectangle in square feet given width 
cout << "4) Compute the area of a rectangle in meters given the length 
cout << "5) Quit the Program" << endl; 
cout << "Please enter a number (1-5)" << endl; 
cin >> choice; 

switch (choice) 
{ 
    //... 
} 

} while(choice != 5); 
+0

好的,謝謝我加了大括號。但是當我在用戶輸入米數後結束時運行代碼。我如何獲得它運行的功能,然後輸出以米爲單位的總米數? – dallascowboys

+0

@dallascowboys查看我更新的帖子。爲什麼你叫dallascowboys而不是dallascowboy?:) –

+0

謝謝你。我是在NFL球隊之後命名的。 – dallascowboys

相關問題