我正在尝试创建一个用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 语言的初学者。有人可以帮我修复此代码吗?
错误:
-
每当您从 stdin 获取任何输入时,您都会采用 &(一个人(这个地址运算符将数据存储到函数中的任何变量中
scanf
因此是一个创建分段错误的错误 -
当您采用任何有符号整数输入时,您必须使用
%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;
}