我在编写此代码时在C ++中遇到问题:
error C2601: 'circle' : local function definitions are illegal
如何找到解决问题的方法?
#include < iostream >
using namespace std;
int main() {
char grade;
cout << "enter grade : ";
cin >> grade;
switch (grade) {
case 'A':
void circle(float &, float &, float);
{
float r, a, c;
cout << "enter radius:";
cin >> r;
circle(a, c, r);
cout << "area=" << a << "\t" << "circumference=" << c << endl;
return 0;
}
void circle(float & area, float & circumference, float r)
{
const double pi = 3.14159;
area = pi * r * r;
circumference = 2 * pi * r;
}
break;
default:
cout << "Invalid grade" << endl;
}
cout << "Your grade is " << grade << endl;
return 0;
}
答案 0 :(得分:4)
它说了什么。在main
函数之外定义函数(和原型!)。
#include < iostream >
void circle(float & area, float & circumference, float r)
{
const double pi = 3.14159;
area = pi * r * r;
circumference = 2 * pi * r;
}
using namespace std;
int main() {
char grade;
cout << "enter grade : ";
cin >> grade;
switch (grade) {
case 'A':
{
float r, a, c;
cout << "enter radius:";
cin >> r;
circle(a, c, r);
cout << "area=" << a << "\t" << "circumference=" << c << endl;
return 0;
}
break;
default:
cout << "Invalid grade" << endl;
}
cout << "Your grade is " << grade << endl;
return 0;
}