我正在尝试创建一个用C编程的计算器。计算器从stdin获取数据。这是我想要的预期结果:
$ ./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的初学者。有人可以帮我解决这个问题吗?
答案 0 :(得分:-1)
错误:
无论何时从stdin获取任何输入,您都需要使用这个地址运算符来将数据存储到scanf
函数中的任何变量中,因此这是一个产生分段错误的错误< / p>
当你接受任何有符号整数输入时,你必须使用
%d
代替%i
这是导致警告的错误
我已经接受了你的程序并编译并删除了错误,下面有一个没有错误的程序。您只需复制并粘贴到编辑器中并检查输出即可 如果有任何问题我会帮你解答我的回复
注意:在检查输出之前,请在代码
下面查看旧文件当前的修改#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("%d", &onethousandpesos);
printf("How many 500 pesos?\n"); scanf("%d", &fivehundredpesos);
printf("How many 200 pesos?\n"); scanf("%d", &twohundredpesos);
printf("How many 100 pesos?\n"); scanf("%d", &onehundredpesos);
printf("How many 50 pesos?\n"); scanf("%d", &fiftypesos);
printf("How many 20 pesos?\n"); scanf("%d", &twentypesos);
printf("How many 10 pesos?\n"); scanf("%d", &tenpesos);
printf("How many 5 pesos?\n"); scanf("%d", &fivepesos);
printf("How many 1 pesos?\n"); scanf("%d", &onepeso);
printf("How many 50 cents?\n"); scanf("%d", &fiftycentavos);
printf("How many 25 cents?\n"); scanf("%d", &twentyfivecentavos);
printf("How many 10 cents?\n"); scanf("%d", &tencentavos);
printf("How many 5 cents?\n"); scanf("%d", &fivecentavos);
printf("How many 1 cents?\n"); scanf("%d", &onecentavo);
printf("Your balance is %d 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;
}