if 字符串比较语句未正确执行

if statement for string comparison is not executing properly

我正在尝试编写一个简单的程序来找出不同的区域 shapes.The 程序编译正常,但是当它运行时它没有给我正确的答案。我的意思是当它运行时它会询问:

What do you want to find area of?

当我输入

square

或其他任何东西,然后按回车键它就结束了,它不会执行任何其他代码。

代码如下

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    char yourchoice[40]="";
    float a=0;; //a=height
    float b=0; //b=breadth
    float Sq,Rec,Parall,trap,cir,ell,tri;

    printf("Here You can find out areas of Square,Rectangle,Parallelogram,trapezoid,circle,ellipse and triangle \n\n\n");

    printf("what do u want to find area of? : \n");
    scanf(" %s",yourchoice);

    if(yourchoice[40]== 'square'){
        printf("Length of any side of the square =");
        scanf(" %f",&a);
        Sq = pow(a,2);
        printf("Area of the Square is %f",&Sq);
    }else if(yourchoice== 'rectangle')
    {
        printf("Height = \n");
        scanf("%f",a);
        printf("Width=\n");
        scanf("%f",b);
        Rec= a*b;
        printf("Area of the Rectangle : %f ",&Rec);

    }
    return 0;
}

你的代码有很多错误。使用 strcmp 而非 ==

比较字符串

你的

if(yourchoice[40]== 'square')

应该是

if(0 == strcmp(yourchoice, "square"))

您不能将 C 字符串与 == 进行比较。你需要 strcmp()'' 用于单个字符,而不是字符串。

所以,改变

if(yourchoice[40]== 'square')

if( !strcmp(yourchoice, "square"))

else if(yourchoice== 'rectangle')

else if(!strcmp(yourchoice, "rectangle"))

顺便说一句,您需要为 strcmp()

添加 <string.h>

另外,改变

printf("Area of the Square is %f",&Sq);

printf("Area of the Square is %f", Sq);
                                   ^ 
                                   no need of &

printf("Area of the Rectangle : %f ",&Rec);

printf("Area of the Rectangle : %f ",Rec);

当您在标识符前放置 & 时,它 returns 是该标识符的地址。您不需要在 printf()

中使用 &

因为你正在使用

if(yourchoice[40]== 'square')

yourchoice[40] 只是您要与字符串进行比较的单个字符。
即使它是错误的,因为你已经声明 char yourchoice[40] 意味着索引将从 039。 使用 strcmp 函数比较字符串。
如果字符串相等,它将 return 0,否则 1-1.
使用

if(strcmp(yourchoice, "square") == 0)

if(!strcmp(yourchoice, "square"))

并且在您的 printf 语句中不要使用 & 来打印变量值。

更改这些行

printf("Area of the Square is %f",&Sq);
printf("Area of the Rectangle : %f ",&Rec); 

printf("Area of the Square is %f",Sq);
printf("Area of the Rectangle : %f ",Rec);

而在您的其他部分,您忘记在 scanf

中添加 &

更改这些行

scanf("%f",a);
scanf("%f",b);

scanf("%f",&a);  // in scanf, '&' is required.
scanf("%f",&b);

第 1 点:

使用 strcmp() 进行字符串比较,而不是 == 运算符。

第 2 点:

无论如何,对于 char yourchoice[40]=""; 的数组,使用 yourchoice[40] 是越界的,这又会调用 undefined behaviourC中的数组索引从0开始。

第 3 点:

printf() 不需要指向数据参数的指针。变化

printf("Area of the Square is %f",&Sq);
printf("Area of the Rectangle : %f ",&Rec);

printf("Area of the Square is %f\n", Sq); //& not required
printf("Area of the Rectangle : %f\n",Rec); //& not required

第 4 点:

scanf() 需要指向数据类型参数的指针。更改

scanf("%f",a);
scanf("%f",b);

scanf("%f", &a); // & is required
scanf("%f", &b); // & is required

第5点: 感谢@Mints97

先生

您需要使用 " " 来表示 字符串文字 ' ' 用来表示一个char。这两个是不同的。

一般建议:

  1. main() 的推荐原型是 int main(void)
  2. 始终初始化所有局部变量。
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>

    using namespace std;

    int main()
    {
    char yourchoice[40]="";
    float a=0;; //a=height
    float b=0; //b=breadth
    float Sq,Rec,Parall,trap,cir,ell,tri;

    printf("Here You can find out areas of Square,Rectangle,Parallelogram,trapezoid,circle,ellipse and triangle \n\n\n");

    printf("what do u want to find area of? : \n");
    scanf(" %s",yourchoice);

//Here, strcmp is the prefered way to compare the strings
//string.h is the header to use it
// It returns 0 when the string match
    //if(yourchoice == 'square'){
    if(!strcmp(yourchoice,"square")){
        printf("Length of any side of the square =");
        scanf(" %f",&a);
        Sq = pow(a,2);
//Second correction: & are to used only when storing the input and not while accessing or displaying it.So, in scanf you have to use but not for printf statements

    printf("Area of the Square is %f",Sq);
    }
    else if(!strcmp(yourchoice,"rectangle"))
    {
        printf("Height = \n");
//Third Correction, in the org code you had missed to write "&" before the variable 'a'
        scanf("%f",&a);
        printf("Width=\n");
//Fourth Correction, in the org code you had missed to write "&" before the variable 'b'
        scanf("%f",&b);
        Rec= a*b;
//Fifth correction: Same as second correction. Not to use '&' sign in printf statements

        printf("Area of the Rectangle : %f ",Rec);

    }
    return 0;
    }