【发布时间】:2018-03-18 11:46:40
【问题描述】:
我正在尝试创建一个用 C 编程的货币计算器。计算器从标准输入获取数据。这是我想要的预期结果:
$ ./alkansiya
Welcome to the Alkansiya Calculator!
How many 1000 pesos?
3
How many 500 pesos?
7
How many 200 pesos?
0
How many 100 pesos?
15
How many 50 pesos?
23
How many 20 pesos?
46
How many 10 pesos?
162
How many 5 pesos?
279
How many 1 pesos?
73
How many 50 cents?
4
How many 25 cents?
1
How many 10 cents?
0
How many 5 cents?
0
How many 1 cents?
0
Your balance is 13158 pesos and 225 centavos
但是,我下面的代码与预期的结果不一样,最终出现了分段错误:
#include <stdio.h>
int main(void)
{
int onethousandpesos = 0;
int fivehundredpesos = 0;
int twohundredpesos = 0;
int onehundredpesos = 0;
int fiftypesos = 0;
int twentypesos = 0;
int tenpesos = 0;
int fivepesos = 0;
int onepeso = 0;
int fiftycentavos = 0;
int twentyfivecentavos = 0;
int tencentavos = 0;
int fivecentavos = 0;
int onecentavo = 0;
printf("Welcome to the Alkansiya Calculator!\n");
printf("How many 1000 pesos?\n");
scanf("%i", onethousandpesos);
printf("How many 500 pesos?\n");
scanf("%i", fivehundredpesos);
printf("How many 200 pesos?\n");
scanf("%i", twohundredpesos);
printf("How many 100 pesos?\n");
scanf("%i", onehundredpesos);
printf("How many 50 pesos?\n");
scanf("%i", fiftypesos);
printf("How many 20 pesos?\n");
scanf("%i", twentypesos);
printf("How many 10 pesos?\n");
scanf("%i", tenpesos);
printf("How many 5 pesos?\n");
scanf("%i", fivepesos);
printf("How many 1 pesos?\n");
scanf("%i", onepeso);
printf("How many 50 cents?\n");
scanf("%i", fiftycentavos);
printf("How many 25 cents?\n");
scanf("%i", twentyfivecentavos);
printf("How many 10 cents?\n");
scanf("%i", tencentavos);
printf("How many 5 cents?\n");
scanf("%i", fivecentavos);
printf("How many 1 cents?\n");
scanf("%i", onecentavo);
printf("Your balance is %i pesos ", (1000*onethousandpesos)+(500*fivehundredpesos)+(200*twohundredpesos)+(100*onehundredpesos)+(50*fiftypesos)+(20*twentypesos)+(10*tenpesos)+(5*fivepesos)+onepeso);
printf("and '''%d''' centavos.\n", fiftycentavos+twentyfivecentavos+tencentavos+fivecentavos+onecentavo);
return 0;
}
GCC 7.3.0 返回以下警告:
alkansiya.c: In function ‘main’:
alkansiya.c:21:11: warning: format ‘%i’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%i", onethousandpesos);
~^
alkansiya.c:23:11: warning: format ‘%i’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%i", fivehundredpesos);
~^
alkansiya.c:25:11: warning: format ‘%i’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%i", twohundredpesos);
~^
alkansiya.c:27:11: warning: format ‘%i’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%i", onehundredpesos);
~^
alkansiya.c:29:11: warning: format ‘%i’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%i", fiftypesos);
~^
alkansiya.c:31:11: warning: format ‘%i’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%i", twentypesos);
~^
alkansiya.c:33:11: warning: format ‘%i’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%i", tenpesos);
~^
alkansiya.c:35:11: warning: format ‘%i’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%i", fivepesos);
~^
alkansiya.c:37:11: warning: format ‘%i’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%i", onepeso);
~^
alkansiya.c:39:11: warning: format ‘%i’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%i", fiftycentavos);
~^
alkansiya.c:41:11: warning: format ‘%i’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%i", twentyfivecentavos);
~^
alkansiya.c:43:11: warning: format ‘%i’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%i", tencentavos);
~^
alkansiya.c:45:11: warning: format ‘%i’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%i", fivecentavos);
~^
alkansiya.c:47:11: warning: format ‘%i’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%i", onecentavo);
我不明白这些警告。我试图修复它,但仍然没有运气。我是 C 初学者。有人可以帮我修复这段代码吗?
【问题讨论】:
-
scanf("%i", foo)->scanf("%i", &foo) -
阅读这些警告,它们会准确地告诉您问题所在。然后回到你的教科书,从头开始。
-
@PaulR 完美!我的计算器可以正常工作。请将其作为答案发布,以便我接受:)
-
scanf%i不推荐。我建议改为%d(实际上,我建议不要使用scanf进行用户输入)。 -
@Someprogrammerdude 我会在学习 C 时始终牢记这一点。好吧,至少我从中学到了一些重要的东西 :)
标签: c