C语言编程提示 “price”: 未声明的标识符 怎么回事? 请各位大侠指点

#include<stdio.h>
int main(){
float price=0;
int Caculate();
int choice;
int off=0;
choice=price/100;
switch(choice){
case 0: off=0; break;
case 1:
case 2: off=1; break;
case 3:
case 4:
case 5: off=2; break;
case 6:
case 7:
case 8:
case 9:
case 10:off=3; break;
}
price=price*(10-off)/10;
printf("The final price is %10.2f",price);
return 0;
}

int Caculate(){
do{
scanf("%f",&price);
price+=price;
}
while(price!=0.0);
return(price);
}

想要实现一个购物结账以及满规定钱数后打折的功能,请各位大人指点!
内个…… 谢谢各位的回答……
可是为什么我运行之后东西都白送了呢……
输出是:The final price is 0.00
还望各位帮忙!
最新回答
浪漫尽失

2025-03-28 00:14:47

首先我讲一下,你的Caculate()函数里面所用到的price变量没有定义,因此会提示标识符price没有定义。。。
其次{代码块}这是定义了一个块所以它里面的变量只能在块里面用,这样的变量称之为局部变量,而你所定义的price在main函数块里,所以在你的Caculate()函数块不能用
#include<stdio.h>
float price=0; /*可以在这个位置定义一个全局变量*/
int main(){
int Caculate();
int choice;
int off=0;
choice=price/100;
switch(choice){
case 0: off=0; break;
case 1:
case 2: off=1; break;
case 3:
case 4:
case 5: off=2; break;
case 6:
case 7:
case 8:
case 9:
case 10:off=3; break;
}
price=price*(10-off)/10;
printf("The final price is %10.2f",price);
return 0;
}

int Caculate(){
/*或者在这个位置定义一个float price*/
do{
scanf("%f",&price);
price+=price;
}
while(price!=0.0);
return(price);
}
姐媞仼,领迗丅

2025-03-28 01:22:30

你的PRICE是在MAIN函数里面申明的 却用在了下面Caculate函数里 所以他说Caculate函数里的PRICE没有申明

你可以把float price=0; 提到MAIN函数外面,申明成全局变量就可以了
它的糖诗

2025-03-28 01:56:12

你那个price是在主函数中声明的,所以在Caculate()中不重新定义就不能用啦!
你可以把price定义为全局变量。
欧巴会发光

2025-03-28 01:51:12

在你的 Calculate 之中,price并没有定义 ,你之前 定义 的price只是在主函数之中使用,所以在int Caculate()中加入 float price即可
青山一叙

2025-03-28 01:21:29

mysort用冒泡,比较的时候把"> < =="用compar函数替换就行了

比如 if( x > y) 换成 if(compar(x,y) > 0)