【问题标题】:Having some trouble creating a C money calculator [duplicate]创建C货币计算器时遇到一些麻烦[重复]
【发布时间】: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", &amp;foo)
  • 阅读这些警告,它们会准确地告诉您问题所在。然后回到你的教科书,从头开始。
  • @PaulR 完美!我的计算器可以正常工作。请将其作为答案发布,以便我接受:)
  • scanf %i 不推荐。我建议改为%d(实际上,我建议不要使用scanf 进行用户输入)。
  • @Someprogrammerdude 我会在学习 C 时始终牢记这一点。好吧,至少我从中学到了一些重要的东西 :)

标签: c


【解决方案1】:

错误:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 2016-09-19
    • 1970-01-01
    • 2020-11-02
    • 2023-03-31
    相关资源
    最近更新 更多