對於一個項目,我必須創建一個簡單的菜單驅動的轉換軟件包,用戶輸入一個數字並轉換其他東西,米到腳,米到米等。我必須爲每次轉換使用一個函數。我已經開始使用米到腳功能。但是當我編譯它時,我收到錯誤消息「之前不允許使用函數定義'('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;
您應該添加,你有你的錯誤的行號內。你的錯誤很簡單:注意你的'switch'在主函數之外,你應該將第一個'}'移動到文件的底部。 –