• 13
• 18
• 19
• 27
• 9

# Problems with my code!

This topic is 3888 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

ok, so I was developing this small text game and it was all going fine until I ran into some problems that I just can't figure out what's wrong with it. I'm a n00b to programming and my code probably has a ton of holes in it and I know it's sloppy, but just try to read it as best you can. Here's the code:
#include <iostream>
using namespace std;

int main()
{
char name[25];
int a;
int b;
int c;

cout << "Welcome to Confinement Break!\n";
cout << "\n\n";
cout << "What is your name?\n";
cout << "   > ";
cin >> name;
cout << "\n\n";
cout << "Welcome to conciousness, " << name << ". You have just awoken on a\n";
cout <<	"table in a small dungeon. To the left of you is a mangled\n";
cout << "corpse on another table. It seems your fate would've been\n";
cout << "similar, had you not been awoken and freed by this\n";
cout << "mysterious force.\n\n";
cout << "You've got to get out of here! There's a door in front\n";
cout << "of you and a pile of clothes near it.\n\n\n";
cout << "What will you do?\n\n";
cout << "1) Go through the door\n";
cout << "2) Examine the pile of clothes\n";
cout << "   > ";
cin >> a;
cout << "\n";

switch (a)
{
case 1:
cout << "\n";
cout << "You have gone through the door. A man in strange attire jumps at you\n";
cout << "and kills you on the spot!\n";
cout << "\n\n\n";
cout << "                               -GAME OVER-\n\n\n";
break;

case 2:
cout << "\n";
cout << "Examining the clothes more closely, you find a trapdoor underneath them.\n";
cout << "\n\n";
cout << "You continue through the trapdoor...\n";
cout << "\n\n\n";
}
cout << "You are now in a very dark passage. You start walking forward while feeling\n";
cout << "along the wall. You see a dim light ahead. Going towards the light, you end\n";
cout << "up in a small study. The light source in the room is a small candle on a\n";
cout << "table. There are three doors leading from this room. One in front of you,\n";
cout << "another to the left, and the last to the right.\n\n\n";
cout << "What will you do?\n\n";
cout << "1) Take the room in front of you\n";
cout << "2) Take the room to the left\n";
cout << "3) Take the room to the right\n";
cout << "   > ";
cin >> b;
cout << "\n";

switch (b)
{
case 1:
cout << "\n";
cout << "You walk through the door in front of you and fall into a pit of spikes\n";
cout << "and die.\n";
cout << "\n\n\n";
cout << "                               -GAME OVER-\n\n\n";
break;

case 2:
cout << "\n";
cout << "You go through the door to the left. A huge block falls from the ceiling\n";
cout << "and crushes you to death.\n";
cout << "\n\n\n";
cout << "                               -GAME OVER-\n\n\n";
break;

case 3:
cout << "\n";
cout << "You take the door to the right and fall down a pit that leads to a slide.\n";
cout << "Whee!\n";

}

cout << "At the end of the slide there is a passageway with a bright light at the\n";
cout << "end of it. This light looks familiar.\n\n";
cout << "What will you do?\n\n";
cout << "1) Walk forth to the light\n";
cout << "   > ";
cin >> c;
cout << "\n";

switch (c)
{
case 1:
cout << "You walk into the light and feel the warmth of the sun and a cool breeze brush\n";
cout << "your face. You did it. You are free!\n";
cout << "\n\n\n";
cout << "                               -YOU WIN!-\n\n\n";
break;
system("Pause");

case 2:
cout << "\n\n\n";
cout << "                               -GAME OVER-\n\n\n";
break;

}

default:
cout << "That option is not valid.\n\n";
cout << "..Terminating program...\n\n\n";
break;

system("Pause");

}


And here's my problem: The first set of choices have no problems whatsoever with them, but after that, if you get to any choices that will lead to game over, it'll say Game Over, but before it says press any key to continue... (to end the game), it shows the "not a valid option" default case. What did I do wrong in my code and how can I fix it? EDIT*: I'm trying something right now to see if it will work. Just give me a sec, and I'll let you know how it goes. =) EDIT**: Yea, didn't work whatsoever, just made it worse, so I put it back to the code above. EDIT***:So many edits hehe. but this is just getting stranger. the code above worked somewhat, but now it's as bad as when i changed it even though it's exactly the same as it was before. AGH

##### Share on other sites
Your switch blocks are broken. Match up all the {s and }s and you'll see that the "default" isn't actually inside any switch block at all.

Note that a "default" block only applies to a single switch. If you've got nested switches - like you do - then it won't apply to all of them, only to the one that it's directly enclosed by.

##### Share on other sites
Yea that's mostly what I figured, but then I thought a few details such as that wouldn't make things that bad. hehe. I might just take all of the text, scrap the code, and start over. or maybe just rework it, but I really don't know where I should put the brackets to.

EDIT*: ARGH! I completely screwed up my code now. I'm just gonna delete and work on it when I actually know what I'm doing.

##### Share on other sites
Quote:
 Original post by IN1L8Yea that's mostly what I figured, but then I thought a few details such as that wouldn't make things that bad. hehe. I might just take all of the text, scrap the code, and start over. or maybe just rework it, but I really don't know where I should put the brackets to.

You can make your life easier by using an editor that can do the formatting to you. Or, you can also make sure to write the closing bracket/brace/parentheses immediately after every open bracket/brace/parenthese you just typed.

##### Share on other sites
Quote:
 Original post by IN1L8Yea that's mostly what I figured, but then I thought a few details such as that wouldn't make things that bad. hehe.

Details are not usually things one can ignore in programming. The compiler is extremely pedantic...

##### Share on other sites
Quote:
 Original post by IN1L8EDIT*: ARGH! I completely screwed up my code now. I'm just gonna delete and work on it when I actually know what I'm doing.

You can make your life easier by using a version control system, such as TortoiseSVN.