传递‘strcmp’的参数 1 和 2 使指针来自整数而不进行强制转换 [-Wint-conversion]

passing argument 1 and 2 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]

你好,我一直在研究一个小程序,它有点像计算器(我是初学者),正如你在代码末尾的小标题中看到的那样,如果 strcmp 没有,这两个不工作。 vscode 告诉我(对于 strcmp)发生了异常。分段故障。但是 gcc 告诉我标题中的内容。

#include <stdio.h>
#include <string.h>

int main()
{

    float num1;
    float num2;
    float anwser;

    int rnum = 1;
    int hi = 0;

    char operator;
    char ifyorn;
    char y = 'y';
    char n = 'n';

    while (hi == 0)
    {

        printf("Enter operator +, -, /, x: ");
        scanf(" %c", &operator);
        printf("Enter num %d :", rnum++);
        scanf("%f", &num1);
        printf("Enter num %d :", rnum++);
        scanf("%f", &num2);

        switch (operator)
        {
        case '+':

            anwser = num1 + num2;
            printf("Do you want to continue y/n \n");
            scanf(" %c", &ifyorn);

            break;

        case '-':

            anwser = num1 - num2;
            printf("Do you want to continue y/n \n");
            scanf(" %c", &ifyorn);

            break;

        case 'x':

            anwser = num1 * num2;
            printf("Do you want to continue y/n \n");
            scanf(" %c", &ifyorn);

            break;

        case '/':

            anwser = num1 / num2;
            printf("Do you want to continue y/n \n");
            scanf(" %c", &ifyorn);

            break;

        default:

            printf("This is not a valid character please try again :(");

            break;
        }

        if(strcmp (ifyorn, n) == 0)
        {
            printf("%f", anwser);
             hi == 1;
        }

        if(strcmp (ifyorn, y) == 0)
        {
             hi == 0;
        }
    }
}

你不必对这个人刻薄,他正在学习。

您收到此错误是因为您将字符传递给 strcmp() 而不是指向字符的指针。

这里有关于该功能的更多信息。

https://www.programiz.com/c-programming/library-function/string.h/strcmp

变量 ifyornyn 的声明类型为 char

char ifyorn;
char y = 'y';
char n = 'n';

函数 strcmp 需要指向字符串的指针类型 char * 的参数。

所以这些 if 语句

if(strcmp (ifyorn, n) == 0)

if(strcmp (ifyorn, y) == 0)

不正确。相反,你应该写

if ( ifyorn == n )

if ( ifyorn == y )

此外,您在这些语句中使用的是比较运算符而不是赋值

 hi == 1;

 hi == 0;

你需要写

 hi = 1;

 hi = 0;

增加变量 rnum 看起来毫无意义

    printf("Enter num %d :", rnum++);
    scanf("%f", &num1);
    printf("Enter num %d :", rnum++);
    scanf("%f", &num2);

为什么不直接写

    printf("Enter num %d :", 1 );
    scanf("%f", &num1);
    printf("Enter num %d :", 2 );
    scanf("%f", &num2);

并且在标签 default 下的代码片段中,您应该再添加一条语句

    default:

        printf("This is not a valid character please try again :(");
        ifyorn = y;
        break;