So I'm new to coding and have been trying to learn C as a base first language. I've hit a block when it comes to the way C handles strings. For example, I tried to write a very simple program that asks the user what shape they're trying to calculate the area for, and then give back the result (as shown below). However, I keep getting an error when I compile the program that says in the parans following switch, that getShape is not an integer. I've looked online, and it appears for a switch statement, the expression (getShape) needs to be an integer? If that's the case, would the only way to write such a program be with a string of if / else statements using strncmp()? Any help is greatly appreciated! (Side note: this isn't for school, I'm trying to learn coding on my own).
#include <stdio.h>
#define PI 3.14
#include <string.h>
int main()
{
float length;
float width;
float diameter;
float height;
float radius = diameter / 2;
float surfaceArea;
float area;
float perimeter;
char *getShape;
strlwr(getShape);
printf("What is your shape?");
scanf("%s", &getShape);
switch (getShape)
{
case "rectangle":
printf("Please enter a length: ");
scanf("%f", &length);
printf("Please enter a width: ");
scanf("%f", &width);
printf(".. calculating, please wait..\n");
area = length * width;
printf("The area of your rectangle is %f\n", area);
break;
case "circle":
printf("Please enter a radius: ");
scanf("%f", &radius);
printf(".. calculating, please wait..\n");
area = PI * radius * radius;
perimeter = 2 * PI * radius;
printf("The area of your circle is %f, and the perimeter is %f", area, perimeter);
}
return 0;
}
#include <stdio.h>
#define PI 3.14
#include <string.h>
int main()
{
float length;
float width;
float diameter;
float height;
float radius = diameter / 2;
float surfaceArea;
float area;
float perimeter;
char *getShape;
strlwr(getShape);
printf("What is your shape?");
scanf("%s", &getShape);
switch (getShape)
{
case "rectangle":
printf("Please enter a length: ");
scanf("%f", &length);
printf("Please enter a width: ");
scanf("%f", &width);
printf(".. calculating, please wait..\n");
area = length * width;
printf("The area of your rectangle is %f\n", area);
break;
case "circle":
printf("Please enter a radius: ");
scanf("%f", &radius);
printf(".. calculating, please wait..\n");
area = PI * radius * radius;
perimeter = 2 * PI * radius;
printf("The area of your circle is %f, and the perimeter is %f", area, perimeter);
}
return 0;
}