If the statement to compare strings does not execute as expected
I am trying to write a simple program to define areas of different shapes. The program compiled ok, but when it works, it doesn't give me the correct answer. I mean when it starts up it asks:
What do you want to find in the area?
and when i type
square
or whatever, and hit enter to finish, 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;
}
source to share
Point 1:
Use strcmp()
for string comparison, not operator ==
.
Point 2:
Either way, for an array, char yourchoice[40]="";
usage is yourchoice[40]
out of scope, which in turn causes undefined behavior . The array index in C
starts at 0.
Point 3:
printf()
it is not necessary to have a pointer to the 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()
a pointer to a data type argument is required. Edit
scanf("%f",a);
scanf("%f",b);
to
scanf("%f", &a); // & is required
scanf("%f", &b); // & is required
Point 5: Thanks to Mr. @ Mints97
You need to use " "
to denote a string literal. ' '
used to represent a char
. These two are different.
General offer:
- The recommended prototype for
main()
isint main(void)
- Always initialize all local variables.
source to share
How do you use
if(yourchoice[40]== 'square')
yourchoice[40]
is just one character that you are comparing to a string.
And even that is wrong, since you declared the char yourchoice[40]
index to be from 0
to 39
. use a function strcmp
to compare strings.
If the 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 statement, printf
don't use the &
variable value to print.
Change this 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 other part, you forget to add &
to yourscanf
Change these lines
scanf("%f",a);
scanf("%f",b);
For
scanf("%f",&a); // in scanf, '&' is required.
scanf("%f",&b);
source to share
You cannot compare C strings with ==
. You need strcmp()
. And it is ''
used for a single character, not a string.
So change
if(yourchoice[40]== 'square')
to
if( !strcmp(yourchoice, "square"))
and
else if(yourchoice== 'rectangle')
to
else if(!strcmp(yourchoice, "rectangle"))
By the way, you need to enable <string.h>
forstrcmp()
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 &
in front of an ID, it returns the address of that ID. You don't need to use &
inprintf()
source to share
#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;
}
source to share