需要帮助制作计算器

-2 c

#include <stdio.h>
#include "funcs.h"

int main(void)
{
        /* varibles */
        float a[3];
        char operation;
        int num;

        /* header print */
        printf("Enter the operation you would like to use\n");
        scanf("%c", &operation);

        /* ifs */
        if(operation == "*") //warning is here
        {       
                printf("How many numbers would you like to use (max 4)\n");
                scanf("%d", &num);

                switch(num)
                {
                        case 2:
                                printf("enter your numbers\n");
                                scanf("%f", &a[0]);
                                scanf("%f", &a[1]);
                                printf(" the answer is %2f %2f", a[0] * a[1]);
                        break;
                }

        }
}
Run Code Online (Sandbox Code Playgroud)

有什么问题?我收到这个错误

Calculator.c: In function 'main':
Calculator.c:16:15: warning: comparison between pointer and integer [enabled by default]
Run Code Online (Sandbox Code Playgroud)

为什么不恭维请帮忙.请快点帮忙

Kar*_*k T 5

试着改变

operation == "*"
Run Code Online (Sandbox Code Playgroud)

operation == '*'
Run Code Online (Sandbox Code Playgroud)

这可能是你的问题,因为字符串文字(带双引号的"*")是const char *(指针),operation而是char(整数).有你的警告.

你修复它是好事,因为如果你忽略它,你会得到极其错误的行为(几乎总是假的),因为你将一个字符与一个带字符的字符串的指针进行比较,而不是你想象的两个字符.

ps - @WhozCraig指出的另一个错误(不是编译器但可能是运行时),你的printf有2个说明符(%2f)但只有1个变量.这最多会导致未定义的行为.

修复为:

printf(" the answer is %2f", a[0] * a[1]);
Run Code Online (Sandbox Code Playgroud)

  • 也许解决这个问题:`printf("答案是%2f%2f",a [0]*a [1])`,因为它的未定义行为传递了一个带有两个说明符和一个参数的格式字符串. (4认同)