Reputation: 3
I'm brand new to programming..im just trying to make my own program to find the volume and surface area of spheres and cylinders. I cant figure out why this program keeps crashing before it will get to the rest of the code. I'm guessing the char*
might be wrong but i cant see why it would be.
int main()
{
char* solid;
char* unit;
printf("Welcome to the Center of Spheres and Cylinders!\n");
printf("Would you like to look at a Sphere or a Cylinder?: ");
scanf("%s", solid);
if(solid == "Cylinder" || solid == "cylinder")
{
printf("You chose to look at a Cylinder.\n");
else if(solid == "Sphere" || solid == "sphere")
{
printf("You chose to look at a Sphere.\n");
it crashes just after I input for scanf.
..when i type in either cylinder or sphere it crashes. Thank you for the help
Upvotes: 0
Views: 111
Reputation: 95
Your program has several "flaws", as pointed out by the others:
The "star" denotes a pointer. A pointer must point to a location in memory, which can be done via malloc(), pointer assignment/manipulation, or explicit bit addresses. For more information on pointers, you can read this: http://pw1.netcom.com/~tjensen/ptr/pointers.htm (But seriously, pointers? You've mentioned you're a beginner in programming; pointers are an advanced concept not only in C, but in computer science as well. Anyways, do not think too much about them at the moment.)
String comparison in C is not done by the simple equality operator, but via bit comparison (which is automagically done by the library). The equality operator (==) compares only primitive types (int, char, etc) but not user-defined types or arrays (a string is a character array). You have to use strcmp() (or strncmp() to compare the first n bytes from an optional offset). You can search Google for the documentation of strcmp() and strncmp() for more information.
Keeping these concepts in mind, your program will be like this:
#include <string.h> /**Contains string manipulation functions; very important **/
#include <ctype.h> /**Contains tolower() **/
int main()
{
char* solid;
char* unit;
printf("Welcome to the Center of Spheres and Cylinders!\n");
printf("Would you like to look at a Sphere or a Cylinder?: ");
scanf("%s", solid);
if(strcmp(tolower(solid), 'cylinder') == 0)
{
printf("You chose to look at a Cylinder.\n");
else if(strcmp(tolower(solid), 'sphere') == 0)
{
printf("You chose to look at a Sphere.\n");
}
/** Other code here **/
return 0;
}
As you might have guessed, tolower() turns a string to lowercase. And additional FYI, an array is a pointer, so the justification in using the "star" notation for storing your input from scanf.
Upvotes: 0
Reputation: 882088
char* solid;
creates a character pointer that points to an arbitrary location (at least for automatic variables, which is what you have in your code). Then, when you try to sccanf
into that location, you're invoking undefined behaviour because there is no valid backing storage.
Something like char solid[100];
would create the backing storage, solving that immediate problem, since it allocates space for the characters to be stored. However, there are at least two other problems with your code.
The first is that you don't compare strings in C with ==
, that merely compares the pointers, not the content behind the pointers. To compare the content, C provides a strcmp
function so, rather than:
if (solid == "something")
you should have:
if (strcmp (solid, "something") == 0)
Some implementations may also provide stricmp
which ignores case so you don't have to do:
if ((strcmp (solid, "something") == 0) || (strcmp (solid, "Something") == 0))
instead going with:
if (stricmp (solid, "something") == 0)
which will allow any characters to be upper or lowercase, such as SomeThing
.
However, that's not standard C so it may not be available everywhere.
Your other major problem lies with scanf("%s")
. Using an unbounded string here is unsafe as you're subject to buffer overflows if the user enters more than you expect. For example, if you use the afore-mentioned char solid[100]
and the user enters five hundred characters, it's likely to trash your stack and cause yet another crash.
If you want a truly robust user input function, have a look at this one. It has overflow protection and throws away the rest of the line if necessary so that subsequent inputs are not affected.
Upvotes: 0
Reputation: 143122
solid
is a character pointer, it is not pointing to any allocated memory location which is causing your program crash when you try to read data into it with scanf()
( and which is why it crashes right after that call as you observe).
After you declare
char *solid;
you should malloc()
a certain amount of storage for it to point to. Alternatively you could have declared an array named solid
char solid[100];
Note that the crash is actually a good thing in that it is helpful to show there's a problem with an errand pointer. This may not always happen unfortunately depending on where a pointer in memory is pointing too.
Upvotes: 3
Reputation: 9396
The problem is with the line
if(solid == "Cylinder" || solid == "cylinder")
U cannot compare strings like that in C. Instead use strcmp library function available in C.
The code should be something like below
if( (strcmp(solid,"Cylinder")==0) || (strcmp(solid,"cylinder")==0) )
Hope this helps.
Upvotes: 0