gpt4 book ai didi

c - 需要c语言帮助制作计算器

转载 作者:行者123 更新时间:2023-11-30 19:55:37 24 4
gpt4 key购买 nike

#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]

为什么无法编译,请帮忙。请立即帮忙

最佳答案

尝试改变

operation == "*"

operation == '*'

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

你解决这个问题是件好事,因为如果你忽略它,你会得到非常错误的行为(几乎总是错误的),因为你正在将一个字符与一个字符串的指针与一个字符进行比较,而不是像你一样将两个字符进行比较会期望。

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

修复为:

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

关于c - 需要c语言帮助制作计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14181897/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com