gborges0727

Honorable
Mar 31, 2013
5
0
10,510
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;
}
 
Solution
You could create an enum for the getShape variable instead and set numeric values for each of the shapes and then use those. Or you could have another variable called shapeInt and put an if statement assigning a number to shapeInt and using shapeInt for the switch.

rishiswaz

Honorable
Mar 10, 2012
5
0
10,520
You could create an enum for the getShape variable instead and set numeric values for each of the shapes and then use those. Or you could have another variable called shapeInt and put an if statement assigning a number to shapeInt and using shapeInt for the switch.
 
Solution

rishiswaz

Honorable
Mar 10, 2012
5
0
10,520
I think it would help to look into what a string actually is, it will help in understanding why you can't switch with a string. In a nutshell a string is an array of characters ending with EoL. This array can't be equated to a string literal because of the array property of it, this is in C++ as well strings dont play nice with switch statements.
 

Ijack

Distinguished
When you learn an object-oriented language, such as C++, you will come upon polymorphism and discover that there are much better ways of handling such situations than switch statements. Personally, I would recommend beginners to start with an OO language (C# is an excellent example) and learn modern programming paradigms straight away. That way there is less to unlearn.
 

Pinhedd

Distinguished
Moderator


Switch flow control methods can only be used on unique integral types (char, short, int, long int, long long int). They enable a number of compiler optimizations which can reduce code size and improve run time variation compared to the more flexible if-elseif-else flow control method. For example, a switch statement may be compiled into a lookup table which can be traversed in near constant time regardless of the input value; by comparison expressions in if-elseif-else blocks need to be evaluated in order until a predicate evaluates to true or the else block is reached. This means that the first instruction corresponding to the last option in a switch statement will not take any longer to reach than the first instruction corresponding to the first option, this is not true for an if-elseif-else method.