# If, Else if, Else statement Problems

Page 1 of 1

## 10 Replies - 3048 Views - Last Post: 23 July 2009 - 02:07 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=116344&amp;s=1b4ed4673cd2acd7efd2f2c527f3b63c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mchristine23

Reputation: 0
• Posts: 4
• Joined: 22-July 09

# If, Else if, Else statement Problems

Posted 22 July 2009 - 11:36 PM

Hey I have a program that suceeded but I just want the one part of my if and else if statements to display not all of them. Please help and thanks for your time.

```#include <stdio.h>

typedef enum Killer_house
{
WAKING,
WAITING,
PROCEEDING,
NEXT,
LAST,
QUIT
} Killer_house;
```

```# include <stdio.h>
# include "Killer.h"

void main ()
{
Killer_house current_status = WAKING;
Killer_house new_status= PROCEEDING;
Killer_house new_status2= WAITING;
Killer_house new_status3= NEXT;
Killer_house new_status4= LAST;
Killer_house new_status5= QUIT;
char choice;
char end= 0;

while(0 == end)
{
switch(current_status)
{
case WAKING:
printf("Your story begins when you and your friends decide to follow a creepy\n man into the haunted, abandoned mashion that screams \"All those who enter die!\".\nBut you all go in\n anyway and the next thing you remember is waking up in a makeshift operating\n room on the operating table.\n");
printf("You have three options and your choices are:\n");
printf(" At this point you have already gone through your intail shocking yelling,\n crying, screaming so it's not an option.\n");
printf(" 1 - Go into the nearest door... maybe it's a way out. By typing in 'p'.\n ");
printf("2 - There is no way in HELL I am going anywhere\n with a pycho on the lose! Type 's'.\n");
printf("3 - Go into the open freezer door where you hear strange noises coming from\n inside and the lights a flickering... maybe its your friends. Type 'e'.\n");
scanf("%c", &choice);

getchar ();

if ('p' == choice)
{
printf("Well you made it safetly to the next room without running into the pycho killer\n but unfortunately your still trapped in a haunted mashion with a pycho killer.");
getchar ();
current_status = PROCEEDING;
}
else if ('s' == choice)
{
printf("Well you are waiting for Prince Charming to come and save you but unfortunately he lives in Far Far Way. So you have to wait a while.\n");
printf("Hit Enter twice to Quit.");
getchar ();
current_status = WAITING;
end=1;
}
else ('e' == choice);
{
printf(" Well the last thing you saw was a menacing figure coming toward you then you fainted.\n Well now your back to the OR on the operating table. Hit Enter to start over.");
getchar ();
current_status = WAKING;
end=1;
}

case NEXT:
printf("Well by some mircale you made it into an empty room with no one but yourself.\n The good news you didn't run into the Pycho who pertends his mother is alive an well but on the other hand you didn't find of your friends or find parts of them for that matter.\n");
printf(" Okay so now that you have escaped the operating room with the strange noises you are not out of the mansion\n or at least in screaming distance of the frequent passerby's. Well the options before are:\n");
printf("Look that door! It's cracked open. Type 'k'.\n");
printf(" Well...\" My mother told me to pick the very best one and you are it!\" and\n the lucky door you happen to pick is the one in the nook and cranny where the light bearly reaches... promising. Type 'g'.\n");
printf(" As soon as you get into the room you find a hiding spot in the bottom cabinets of a china cabinet praying the killer won't find you and that the police will. Type 'd'.");
scanf("%c", &choice);
getchar ();

if ('g' == choice)
{
printf("Well you made it safetly to the next room without running into the pycho killer\n even though that door had \" Don't go in me\" written all over it. Now if you get out of this alive I presume you will thanking your mother BUT your still trapped with a pycho killer on you six.");
getchar ();
current_status = PROCEEDING;
}
else if ('k' == choice)
{
printf(" As you come to, on the dreaded operating table you begin to remember that there happened\n to be a fresh blood stain on the frame of the door but that was after the pycho killer knocked you out. Back to the drawing board.");
getchar ();
current_status = WAKING;
}
else ('d' == choice);
{
printf("Well I'm sorry to say the men in blue are dealing with other things at the moment and\n on top of that there won't be a missing persons report until a week from now. Remember your parents are away for their 25th Anniversary in the Andes Mountians with no signal. Hope you had a big meal before this because it's going to be a while before anyone knows your missing.");
printf("Hit Enter twice to Quit.");
getchar ();
current_status = WAITING;
end=1;
}

case LAST:
printf("Your almost out of this madness, your so close you can hear the faint screams of the kids playing down the street, at least you hope it's that.\n");
printf("The room you have just entered is nothing like any you have been in previously.\n This room is ironically as dark as the makeshift OR room and as grand as a ballroom and also has a \"Welcoming\" and Gothic type feel to it with dust, cobbwebs,and weapons all over the walls.\n");
printf(" Unfortunately there are no doors to pick from to go through only the one you came through.\n");
printf("What will you do?\n");
printf("Well it's obvisously hopeless... So you figure you will just wait for the killer, who's name you just recently found out from finding his name tag is Lars Zoolander Ripper. Type 'w'.\n");
printf("Look around for something, anything from giving up hope. Type 't'.\n");
printf("Go back out the way you came and find another way to get out. Type 'j'.\n");
scanf("%c", &choice);

getchar ();

if ('t'  == choice)
{
printf("Well in your frantic state to keep hope you found it by looking up and seeing a\n hidden pole that you can climb out to safety. You live to tale the tell, call the police, and thank your mom for chosing the best one. Next time with your new friends don't let them go into the abandoned haunted masion after a psycho like person OR you could just leave them to their fates. Your choice.");
getchar ();
current_status = QUIT;
end=1;
}
else if ('j' == choice)
{
printf("Even though it was the logical thing to do you still have found yourself coming to, on the operation table.");
getchar ();
current_status= WAKING;
}
else ('w' == choice);
{
printf("Okay so even though you are waiting to meet your end you will never find it.");
printf("Hit Enter twice to Quit.");
getchar ();
current_status = WAITING;
end=1;
}
break;

default:
current_status = QUIT;

break;
}
}
}
```

Is This A Good Question/Topic? 0

## Replies To: If, Else if, Else statement Problems

### #2 no2pencil

• Professor Snuggly Pants

Reputation: 6552
• Posts: 30,682
• Joined: 10-May 07

## Re: If, Else if, Else statement Problems

Posted 22 July 2009 - 11:47 PM

I would use the following :

```int completed=0;
while(completed==0) {
if ('p' == choice)
{
printf("Well you made it safetly to the next room without running into the pycho killer\n but unfortunately your still trapped in a haunted mashion with a pycho killer.");
getchar ();
current_status = PROCEEDING;
completed=1;
}
if ('s' == choice)
{
printf("Well you are waiting for Prince Charming to come and save you but unfortunately he lives in Far Far Way. So you have to wait a while.\n");
printf("Hit Enter twice to Quit.");
getchar ();
current_status = WAITING;
end=1;
completed=1;
}
if ('e' == choice);
{
printf(" Well the last thing you saw was a menacing figure coming toward you then you fainted.\n Well now your back to the OR on the operating table. Hit Enter to start over.");
getchar ();
current_status = WAKING;
end=1;
completed=1;
}
}

```

### #3 mchristine23

Reputation: 0
• Posts: 4
• Joined: 22-July 09

## Re: If, Else if, Else statement Problems

Posted 22 July 2009 - 11:56 PM

no2pencil, on 22 Jul, 2009 - 10:47 PM, said:

I would use the following :

```int completed=0;
while(completed==0) {
if ('p' == choice)
{
printf("Well you made it safetly to the next room without running into the pycho killer\n but unfortunately your still trapped in a haunted mashion with a pycho killer.");
getchar ();
current_status = PROCEEDING;
completed=1;
}
if ('s' == choice)
{
printf("Well you are waiting for Prince Charming to come and save you but unfortunately he lives in Far Far Way. So you have to wait a while.\n");
printf("Hit Enter twice to Quit.");
getchar ();
current_status = WAITING;
end=1;
completed=1;
}
if ('e' == choice);
{
printf(" Well the last thing you saw was a menacing figure coming toward you then you fainted.\n Well now your back to the OR on the operating table. Hit Enter to start over.");
getchar ();
current_status = WAKING;
end=1;
completed=1;
}
}

```

Are you saying not use char end=0 and get rid of the cases and the switch? Because I have to use the Cases and switch function.

### #4 no2pencil

• Professor Snuggly Pants

Reputation: 6552
• Posts: 30,682
• Joined: 10-May 07

## Re: If, Else if, Else statement Problems

Posted 23 July 2009 - 12:00 AM

I guess I'm not sure which if statements you are referring to then. Can you go over what code you are asking about?

### #5 mchristine23

Reputation: 0
• Posts: 4
• Joined: 22-July 09

## Re: If, Else if, Else statement Problems

Posted 23 July 2009 - 12:08 AM

no2pencil, on 22 Jul, 2009 - 11:00 PM, said:

I guess I'm not sure which if statements you are referring to then. Can you go over what code you are asking about?

I'm refering to the code below. Does the char end=0 replaced with the int completed=0. When you ran the program how did you stop all of the if statements from showing up after you hit enter and then moving on to the next case? I just need to know how to get just the one if statement to show and move on to the next case (regarding the original code).

```char choice;
char end= 0;

while(0 == end)
{
switch(current_status)
{
case WAKING:
printf("Your story begins when you and your friends decide to follow a creepy\n man into the haunted, abandoned mashion that screams \"All those who enter die!\".\nBut you all go in\n anyway and the next thing you remember is waking up in a makeshift operating\n room on the operating table.\n");
printf("You have three options and your choices are:\n");
printf(" At this point you have already gone through your intail shocking yelling,\n crying, screaming so it's not an option.\n");
printf(" 1 - Go into the nearest door... maybe it's a way out. By typing in 'p'.\n ");
printf("2 - There is no way in HELL I am going anywhere\n with a pycho on the lose! Type 's'.\n");
printf("3 - Go into the open freezer door where you hear strange noises coming from\n inside and the lights a flickering... maybe its your friends. Type 'e'.\n");
scanf("%c", &choice);

getchar ();

if ('p' == choice)
{
printf("Well you made it safetly to the next room without running into the pycho killer\n but unfortunately your still trapped in a haunted mashion with a pycho killer.");
getchar ();
current_status = PROCEEDING;
}
else if ('s' == choice)
{
printf("Well you are waiting for Prince Charming to come and save you but unfortunately he lives in Far Far Way. So you have to wait a while.\n");
printf("Hit Enter twice to Quit.");
getchar ();
current_status = WAITING;
end=1;
}
else ('e' == choice);
{
printf(" Well the last thing you saw was a menacing figure coming toward you then you fainted.\n Well now your back to the OR on the operating table. Hit Enter to start over.\n");
getchar ();
current_status = WAKING;
end=1;
}

```

### #6 no2pencil

• Professor Snuggly Pants

Reputation: 6552
• Posts: 30,682
• Joined: 10-May 07

## Re: If, Else if, Else statement Problems

Posted 23 July 2009 - 12:12 AM

Since you are checking the same variable ( choice ), there is no one that a single char can be more than one letter, so it will never hit more than one of those conditions.

Rather than use if, else if, else, I would just do three if statements ( like I suggested previously ), as only one of them are going to be a match. I would also use the while loop ( as suggested ) in the event that they provided an invalid character. That means you'll need to put your scanf inside of the while loop.

• D.I.C Regular

Reputation: 9
• Posts: 419
• Joined: 08-April 09

## Re: If, Else if, Else statement Problems

Posted 23 July 2009 - 12:15 AM

just a thought,

```		  if ('p' == choice)
```

swap them around like this
```		if (choice == 'p')
```

### #8 mchristine23

Reputation: 0
• Posts: 4
• Joined: 22-July 09

## Re: If, Else if, Else statement Problems

Posted 23 July 2009 - 12:19 AM

no2pencil, on 22 Jul, 2009 - 11:12 PM, said:

Since you are checking the same variable ( choice ), there is no one that a single char can be more than one letter, so it will never hit more than one of those conditions.

Rather than use if, else if, else, I would just do three if statements ( like I suggested previously ), as only one of them are going to be a match. I would also use the while loop ( as suggested ) in the event that they provided an invalid character. That means you'll need to put your scanf inside of the while loop.

Okay. Is there anyother way to do this or just that way?

ladyinblack, on 22 Jul, 2009 - 11:15 PM, said:

just a thought,

```		  if ('p' == choice)
```

swap them around like this
```		if (choice == 'p')
```

Yes and it didn't make a difference.

• D.I.C Regular

Reputation: 9
• Posts: 419
• Joined: 08-April 09

## Re: If, Else if, Else statement Problems

Posted 23 July 2009 - 12:39 AM

then try getting rid of the else and see what happens.

Reputation:

## Re: If, Else if, Else statement Problems

Posted 23 July 2009 - 12:44 AM

this is incorrect
else ('e' == choice);

should be
else if ('e' == choice)

in every if ... else if ... else ... will run only one section ...

• D.I.C Regular

Reputation: 9
• Posts: 419
• Joined: 08-April 09

## Re: If, Else if, Else statement Problems

Posted 23 July 2009 - 02:07 AM

Now, why didn't you see that.