gurpinder singh
gurpinder singh

Reputation: 29

if statement for string comparison is not executing properly

I am trying to write a simple program for finding out areas of different shapes.The program is compiled fine but when it runs it does not give me the right answer. I mean When it runs it asks:

What do you want to find area of?

and when I type

square

or anything else and hit enter it just ends,it doesn't execute any other code.

The code is below.

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

Upvotes: 3

Views: 115

Answers (5)

Sourav Ghosh
Sourav Ghosh

Reputation: 134336

Point 1:

Use strcmp() for string comparison, not == operator.

Point 2:

Anyways, for an array of char yourchoice[40]="";, using yourchoice[40] is out of bounds which in turn invokes undefined behaviour. Array index in C starts from 0.

Point 3:

printf() does not need to have a pointer to data argument. Change

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

to

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

Point 4:

scanf() requires a pointer to data type argument .Change

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

to

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

Point 5: With thanks to Mr. @Mints97

You need to use " " to denote a string literal. ' ' is used to represent a char. These two are different.

General Suggestion:

  1. The recommended prototype for main() is int main(void)
  2. Always initialize all of your local variables.

Upvotes: 4

letsBeePolite
letsBeePolite

Reputation: 2251

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

Upvotes: 0

Himanshu
Himanshu

Reputation: 4395

As you are using

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

yourchoice[40] is only a single character you are comparing with a string.
And even it is wrong as you have declared char yourchoice[40] means index will be from 0 to 39. use strcmp function to compare strings.
If strings are equal it will return 0, otherwise 1 or -1.
Use

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

Or

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

And in your printf statement don't use & to print variable value.

Change these line

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

to

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

And in your else part you forget to add & in your scanf

Change these lines

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

To

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

Upvotes: 2

Arun A S
Arun A S

Reputation: 7006

You cannot compare C strings with ==. You need strcmp(). And '' is used for a single character, not for a string.

So, change

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

to

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

and

else if(yourchoice== 'rectangle')

to

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

BTW, you need to include <string.h> for strcmp()

Also, change

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

to

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

and

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

to

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

When you put & before an identifier, it returns the address of that identifier. You don't need to use & in printf()

Upvotes: 1

Raghu Srikanth Reddy
Raghu Srikanth Reddy

Reputation: 2711

There are many mistakes in your code. strings are compared using strcmp and not ==

your

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

should be

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

Upvotes: 1

Related Questions