I am a newbie to C, and I am creating a program that helps users calculate SA, Vol etc. I am trying to compare variable choice_1 to "1", but it gives me this* error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Wstring-compare] gcc [Ln 19,Col 18]*
Here is the code:
//
//
//
#include<stdio.h> //Preprocessor Code
#include <string.h>
#define PI 3.14159 // PI is a constant value hence it is better to define it to 1.Save time 2.Prevent Errors 3.Code Readability
int main()
{
char name[100],choice_1[100],_2dshape,_3dshape[100];
float length,breadth,SA,perimeter,diagonal1,diagonal2;
printf("What should we call you?\n>>");
scanf("%s",name);
printf("Hello %s :)\nThis Program will help you calculate the SA ,Volume , Perimeter and Circumference of your geometric shapes!\nInput your choice below:\n",name);
printf("What Shapes are we looking at?\n(1)2 Dimensional (2)3 Dimensional\n>>");
scanf("%s",choice_1);
if (choice_1 == "1")
{
printf("Which Shape's Area and Perimeter do you want to find out?\n1.Square 2.Rectangle 3.Triangle 4.Rhombus 5.Parallelogram 6.Kite 7.Trapazoid\n>>");
scanf("%c",&_2dshape);
switch(_2dshape){
case '1':{
printf("You chose square\nThe formula of perimeter of square is 4*length/breadth and that of area of square is length*breadth\nEnter the length of each side of square below:\n>>");
scanf("%f",&length);
perimeter = 4*length;
printf("The Perimeter of Square is: %f units\n",perimeter);
SA = length*length;
printf("The Area of Square is: %f units\n",SA);
}
case '2':{
printf("You chose Rectangle\nThe formula of perimeter of Rectangle is 2*(length+breadth) and that of area of Rectangle is length*breadth\nEnter the values of length and breadth below:\n>>");
scanf("%f\n%f",&length,&breadth);
perimeter = 2*(length+breadth);
printf("The Perimeter of Rectangle is: %f units\n",perimeter);
SA = length*breadth;
printf("The Area of Rectangle is: %f units\n",SA);
}
case '3':{
printf("You chose Triangle\nThe formula of perimeter of Triangle is 3*length and that of area of Triangle is (base*height)/2\nEnter the values of length and height below:\n>>");
scanf("%f\n%f",&length,&breadth);
perimeter = 3*length;
printf("The Perimeter of Triangle is: %f units\n",perimeter);
SA = length*breadth;
printf("The Area of Triangle is: %f units\n",SA);
}
case '4':{
printf("You chose Rhombus\nThe formula of perimeter of Rhombus is 4*length and that of area of Rhombus is product of diagonals\nEnter the values of diagonals below:\n>>");
scanf("%f",&length);
perimeter = (4*length);
printf("The Perimeter of Rhombus is: %f units\n",perimeter);
printf("Enter the values of diagonals below:\n>>");
scanf("%f\n%f",&diagonal1,&diagonal2);
SA = (diagonal1*diagonal2);
printf("The Area of Rhombus is: %f units\n",SA);
}
case '5':{
printf("You chose Parallelorgram\nThe formula of perimeter of Parallelogram is 2*(sum of a pair of non opposite sides) and that of area of parallelogram is base*height\nEnter the values of side 1 and side 2 below:\n>>");
scanf("%f%f",&length,&breadth);
perimeter = 2*(length+breadth);
printf("The Perimeter of Parallelogram is: %f units\n",perimeter);
printf("Enter the values of height and base below:\n>>");
scanf("%f%f",&diagonal1,&diagonal2);
SA = diagonal1*diagonal2;
printf("The Area of Parallelogram is: %f units\n",SA);
}
case '6':{
printf("You chose kite\nThe formula of perimeter of kite is 2*(sum of two unequal sides) and that of area of kite is length*breadth\nEnter the values of length and breadth below:\n>>");
scanf("%f%f",&length,&breadth);
perimeter = 2*(length+breadth);
printf("The Perimeter of kite is: %f units\n",perimeter);
SA = diagonal1*diagonal2;
printf("The Area of kite is: %f units\n",SA);
}
case '7':{
printf("You chose trapazoid\nThe formula of perimeter of trapazoid is sum of all sides and that of area of trapazoid is product of height and sum of base and longest whole divided by two (b+d)*h/2\nEnter the values of sides below:\n>>");
scanf("%f%f%f%f",&length,&breadth,&diagonal1,&diagonal2);
perimeter = length+breadth+diagonal1+diagonal2;
printf("The Perimeter of trapezoid is: %f units\n",perimeter);
printf("Enter the values of base , longest side and height");
scanf("%f%f%f",&diagonal1,&diagonal2,&length);
SA = length*diagonal1*diagonal2;
printf("The Area of trapezoid is: %f units\n",SA);
}
}
}
}
Let the user input 1 or 2 for 2d or 3d object shapes. I am using if to check whether the choice is 1 or 2. But the compiler throws an error saying result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Wstring-compare]