Invalid if statement?

Hello all,
I am relatively new to programming and ave encountered a problem in a simple text base RPG I am writing. The program compiles without error however the program does not function as I intended. I am running Windows 7 64-Bit using Pelles C to compile my code.

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>

int main(void)
{
	printf("MRPG - My Role Playing Game");
	printf("\n\n\nYou are in a desert wasteland, with the sound of harsh winds rushing past your ears\n What shall you do?\n");
	char a;
	scanf ("%s", & a);

	if ("walk" == "%s", & a)
	{
		printf("You decide to walk, you walk for 3 miles until you feint in the devastating heat.");
	}

	else if ("shout" == "%s", & a)
	{
		printf("You call for help with all of your effort, but after half an hour the eerie silence of the sandy desert only heightens the doubt, rising from your stomach");
	}

	else
	{
		printf("You cannot do that right now ,try walking");
	}
}


The problem i have encountered is that the initial if statement (if ("walk" == "%s", & a)) is run no mater what I input into the scanf.

So if I were to type in "help", "printf("You decide to walk, you...") " is the function carried out by the system.

I just want the user to get one result when typing one thing, and another result when typing anything else.

I apologize if the solution to my dilemma is simple, I have little experience in C programming.

Thanks in advance :D!
if ("walk" == "%s", & a) Will always evaluate to true.

You single char cannot hold what you are asking it to hold. Change that to an array of char or a string.

Change your if's..
if (strcmp("walk",a) == 0)
Thank you for the reply clanmjc :)

My code is now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>

int main(void)
{
	printf("TRPG - Tyler's Role Playing Game");
	printf("\n\n\nYou are in a desert wasteland, with the sound of Tony's grunt booming around you\n What shall you do?\n");
	char a;
	scanf ("%s", & a);

	if (strcmp("walk",a) == 0)
	{
		printf("You decide to walk, you walk for 3 miles until you notice a damp sensation in your shoes. You take off your left shoe, to discover a blood stained sock.");
	}

	else if (strcmp("shout",a) == 0)
	{
		printf("You call for help with all of your effort, but after half an hour the eerie silence of the sandy desert only heightens the doubt, rising from your stomach");
	}

	else
	{
		printf("You cannot do that right now ,try walking");
	}
}


Now when i enter text into the console the program ends, and doesn't run any functions.
What have i done wrong?
Topic archived. No new replies allowed.