通过‘ strcmp&#x2019的参数1和2从整数中制作指针,而无需演员[-wint转换]

发布于 2025-01-31 07:45:56 字数 1657 浏览 3 评论 0原文

您好,所以我一直在工作一些prgramme,这是一个计算器(我是初学者),并且在当时的代码结束时可以看到,如果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;
        }
    }
}

Hello so I've been working a little prgramme which is sort of a calculator (I'm a beginner) and well as you can see in the tittle at then end of the code, the two if strcmp doesn't work. And vscode is telling me (for the strcmp) Exception has occurred. Segmentation fault. But gcc is telling me what is in the tittle.

#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;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不离久伴 2025-02-07 07:45:56

变量ifyornyn被声明具有类型char

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

函数strcmp期望指示指向字符串的指针类型char *的参数。

因此,如果陈述

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:

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

The variables ifyorn, y and n are declared having the type char.

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

The function strcmp expects arguments of the pointer type char * that point to strings.

So these if statements

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

and

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

are incorrect. Instead you should write

if ( ifyorn == n )

and

if ( ifyorn == y )

Also instead of assignments you are using the comparison operator in these statements

 hi == 1;

and

 hi == 0;

You need to write

 hi = 1;

and

 hi = 0;

Increasing the variable rnum looks senseless

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

Why not just to write

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

And in the code snippet under the label default you should add one more statement

    default:

        printf("This is not a valid character please try again :(");
        ifyorn = y;
        break;
锦上情书 2025-02-07 07:45:56

您不必对这个家伙刻薄,他正在学习。

您会遇到此错误,因为您将字符传递到strcmp()而不是指针到字符。

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

https://www.programiz.com/c - 编程/库 - 功能/string.h/strcmp

You don't have to be mean to the guy ,he is learning.

You are getting this error because you are passing characters to strcmp() instead of pointers to characters.

Here is more information regarding that function.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文