需要帮助制作计算器

时间:2013-01-06 12:05:28

标签: 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;
                }

        }
}

问题是什么? 我收到此错误

Calculator.c: In function 'main':
Calculator.c:16:15: warning: comparison between pointer and integer [enabled by default]

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

2 个答案:

答案 0 :(得分:5)

尝试更改

operation == "*"

operation == '*'

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

你正在解决这个问题很好,因为如果你忽略它,你会得到极其错误的行为(几乎总是假的),因为你要将一个字符与指向一个带字符的字符串的指针进行比较,而不是两个字符。期待。

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

修复为:

printf(" the answer is %2f", a[0] * a[1]);

答案 1 :(得分:2)

您应该使用operation == "*"更改operation == '*',因为单引号适用于字符双引号适用于字符串显然,你正在检查字符是否相等。