【问题标题】:Why isn't the calculator I'm coding with C working?为什么我用 C 编写的计算器不能工作?
【发布时间】:2020-12-14 03:30:19
【问题描述】:

我对 C 很陌生,并且正在从事一个相当大的项目。我正在尝试制作一个计算器,到目前为止我只有加法。他们的工作方式是在加法语句中询问他们想要多少个数字,然后我使用 while() 循环来尝试获取所有信息。只是一些附带信息 - 我在 Windows 10 操作系统上使用 Cygwin 终端。

#include <stdio.h>

int main(){
        char Etype;
        printf("Hello! Welcome to your virtual calculator!\n");

        printf("Press 'a' for addition!\n");
        scanf("%c", &Etype);

        if(Etype == 'a' || Etype == 'A') {
                int Anum;
                int x = 1;
                int y;

                printf("How many numbers do you want in this addition statement?\n");
                scanf("%f", &Anum);

                while(x < Anum) {
                        printf("Emter number %d\n", x);
                        scanf("%d", &y);
                        x = x + 1;
                }
        }

}

每当我回答我想要在我的陈述中包含多少个数字时,什么都没有发生。希望您能帮到您,如果可以,谢谢!

【问题讨论】:

  • 您的编译器至少没有告诉您一个主要问题?打开警告 (-Wall -Wextra) 并重新编译。
  • %f 格式应该读取什么类型的值? Anum的类型是什么?
  • 谢谢!我在编码方面太糟糕了哈哈(o-o)
  • Shawn 我没有 -Wall -Wextra 在我的编译器上工作
  • 您使用哪个编译器?你有没有发现如何让它给你常规错误的警告(或者,更好的是,如何让它给你错误)?如果没有,现在花点时间了解如何提高警告,以便您了解代码中的错误。然后注意警告——编译器比你更了解 C。如果您无法让当前的编译器发出警告,请切换到更好的编译器。

标签: c while-loop calculator


【解决方案1】:

至少,您应该查看编译器警告,或者获得更好的编译器并查看编译器警告。

clang-7 -pthread -lm -o main main.c
main.c:16:29: warning: format specifies type 'float *' but the argument has type
      'int *' [-Wformat]
                scanf("%f", &Anum);
                       ~~   ^~~~~
                       %d
1 warning generated.

【讨论】:

  • 这可能与我使用 vim 的事实有关?
【解决方案2】:

也许你只是忘了总结答案。

要输入整数,您应该使用%d 而不是%f

更重要的是,x 应该用0 初始化。

您可以将代码编辑为:

#include <stdio.h>

int main(){
        char Etype;
        printf("Hello! Welcome to your virtual calculator!\n");

        printf("Press 'a' for addition!\n");
        scanf("%c", &Etype);

        if(Etype == 'a' || Etype == 'A') {
                int Anum;
                int x = 0; // count from 0
                int y;
                int sum = 0; // to summary the answer

                printf("How many numbers do you want in this addition statement?\n");
                scanf("%d", &Anum); // use %d to input integer

                while(x < Anum) {
                        printf("Emter number %d\n", x);
                        scanf("%d", &y);
                        sum += y; // summary
                        x = x + 1; 
                }
                printf("Answer: %d\n", sum); // print the answer after calculation
        }

}

或者更清晰的版本:

#include <stdio.h>

int main(){
        char eType[2];

        printf("Hello! Welcome to your virtual calculator!\n");
        printf("Press 'a' for addition!\n");

        scanf("%s", eType);

        if(eType[0] == 'a' || eType[0] == 'A') 
        {
                int n, sum = 0;

                printf("How many numbers do you want in this addition statement?\n");
                scanf("%d", &n);

                for (int i = 0; i < n; i++)
                {
                        int x;

                        printf("Enter number %d\n", i);
                        scanf("%d", &x);
                        sum += x;
                }

                printf("Answer: %d\n", sum);
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    相关资源
    最近更新 更多