c - 创建 C 货币计算器时遇到一些问题

标签 c

我正在尝试创建一个用 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 初学者。有人可以帮我修复这段代码吗?

最佳答案

错误:

  1. 每当您从标准输入获取任何输入时,您都会使用 &(a person) 这个地址运算符将数据存储到 scanf 函数中的任何变量中,因此这是一个创建分段的错误错误

  2. 当您获取任何有符号整数输入时,您必须使用 %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; 
}

关于c - 创建 C 货币计算器时遇到一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49347489/

相关文章:

c++ - SIMD 程序运行缓慢

c - HackerRank 说 ~ 标准输出没有响应 ~ C

c - 二进制转换代码 段错误

c - 如何解决 "unrecognized command line option ' -std=gnu1 1'"错误?

C警告: initialization makes pointer from integer without a cast [enabled by default]

c - 用 popen() 打开的文件没有 EOF?

c - 将 sprintf 与 char * 一起使用

c - 如何将多个 *argv 组合成一个 char* 类型的消息

c++ - 如何在多个平台上使用__FILE__和__LINE__ info实现C/C++可变参数记录宏?

c++ - 这是什么意思?这是关于c++中的DWORD变量