VC的菜鸟问题

来源:百度知道 编辑:UC知道 时间:2024/06/21 03:27:28
#include <stdio.h>
#define PI 3.1415926

void getArea(){
float area=PI*r*r;
printf("该圆面积为%.2f",area);
}

void getCircum(){
float circum=PI*r*r;
printf("该圆周长为%.2f",circum);
}

void main()
{
int k;
float r;

printf("请输入k值:\n");
scanf("%d",&k);

switch(k){
case 1:getArea();break;
case 2:getCircum();break;
case 3:{getArea();getCircum();break;}
default:printf("\n你的输入有误\n");break;
}

}

--------------------Configuration: xxyxxy - Win32 Debug--------------------
Compiling...
a.c
D:\study\c++\Microsoft Visual Studio\MyProjects\xxyxxy\a.c(5) : error C2065: 'r' : undeclared identifier
D:\study\c++\Microsoft Visual Studio\MyProjects\xxyxxy\a.c(5) : warning C4244: 'initializing' : conversion f

getArea函数中

float area=PI*r*r;里面的 r 没有声明,且参数表里也没有 r 啊
所以编译器报错

你需要将getArea()
改为getArea(float r)并在调用的时候传参

以下的几个函数同样需要如此

void getArea(float r){
float area=PI*r*r;
printf("该圆面积为%.2f",area);
}

void getCircum(float r){
float circum=PI*r*r;
printf("该圆周长为%.2f",circum);
}
如上修改函数,加上参数。
下面调用函数,要传入参数。
如getArea(r);
将float改成double就不会用warning了
因为你的PI是double型

r没有声明..需要用参数传递到GETAREA和GETCIRCUM函数里..
void getArea(float r){
float area=PI*r*r;
printf("该圆面积为%.2f",area);
}

void getCircum(float r){
float circum=PI*r*r;
printf("该圆周长为%.2f",circum);
}

1.声明应该在利用前面"r'不应该在main中 应该在前面就声明2.最好不要用单精度浮点而直接双精度浮点..这样避免数据不精确 上面工告诉你有一处错误 两处警告 D:\study\c++\Microsoft Visual Studio\MyProjects\xxyxxy\a.c(10) : warning 这里就是在警告你啊 所以下次直接用double