#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; }