错误 - {token

时间:2018-02-02 03:17:07

标签: c++

此项目的代码正在运行,除了这一个错误。我知道它与int main()有关,但我不确定如何解决它。第19,23,27行。

#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>
using namespace std;

// FUNCTION PROTOTYPES
double radius;
double area;
double circumference;
double diameter;
char letter;

// FUNCTION DEFINITIONS
int main() {

    cout<<"Please enter an 'A' to compute the area, a 'C' to compute the circumference, or a 'D' to compute the diameter, followed by the radius of circle: ";
    cin>>letter>>radius;

double area (double radius){    
     double area=M_PI*(radius*radius);
        return area;
}
double circumference (double radius){
    double circumference=2*(M_PI*radius);
        return circumference;
}
double diameter (double radius){    
     double diameter=2*radius;
        return diameter;
}

switch(letter){
    case 'A':
        cout<<"Area of circle: "<<area<<"\n";
        break;
    case 'C':
    cout<<"Circumference of circle: "<<circumference<<"\n";
            break;
    case 'D':
    cout<<"Diameter of circle: "<<diameter;
            break;
            }   
            }

2 个答案:

答案 0 :(得分:1)

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

double area(double radius);
double circumference(double radius);
double diameter(double radius);

int main(){
    // FUNCTION PROTOTYPES
    double radius;
    double a;
    double c;
    double d;
    char letter;

    cout<<"Please enter an 'A' to compute the area, a 'C' to compute the circumference, or a 'D' to compute the diameter, followed by the radius of circle: ";
    cin>>letter>>radius;

    switch(letter){
        case 'A':
            //call-function here
            a = area(radius);
            cout<<"Area of circle: "<<a<<"\n";
            break;
        case 'C':
            //call-function here
            c = circumference(radius);
            cout<<"Circumference of circle: "<<c<<"\n";
            break;
        case 'D':
            //call-function here
            d = diameter(radius);
            cout<<"Diameter of circle: "<<d;
            break;
    }
}

double area (double radius){    
    double area=M_PI*(radius*radius);
    return area;
}
double circumference (double radius){
    double circumference=2*(M_PI*radius);
    return circumference;
}
double diameter (double radius){    
    double diameter=2*radius;
    return diameter;
}

你需要在main()和下面的函数之前声明函数符号。

答案 1 :(得分:0)

您无法在函数中定义函数(包括main函数)。您需要在main之前执行此操作,或在main之前声明函数签名,并在main之后实现该函数。