I'am running this program on code blocks,
I don't know whats wrong with the code it might be confusing to explain, I recommend you run it to know what I mean. when you choose the first path (1)
it goes along with what it's supposed to do. but when you make the second choice of the of the first path (1>1 or 1>2) it reads: "you stumble across a fork in the road even though that's dialogue for the second path. please help I don't know whats going on.
#include <iostream>
#include <conio.h>
#include <stdlib.h>
usingnamespace std;
int main()
{
char name[50];
cout << "They're coming! Yells the scout." << endl;
cout << "Hold down the gates as long as you can!" << endl;
cout << "you see an army of white cross imperials" << endl;
cout<< "(or locusts as they're called) advancing up the hill." << endl;
cout << " " << endl;
cout << "<---------------------------->" << endl;
cout << " " << endl;
cout << "Okay before we can move on with the story," << endl;
cout << "I'am gonna have to kinda know your name, being the narrator and all." << endl;
cin.getline(name, 50);
cout << " " << endl;
cout << "You better move fast, "<< name <<". The locusts are attacking the city." << endl;
cout << "\n----------------------Press any key to continue----------------------" << endl;
_getch();
system("cls");
int choiceOne_Path;
cout << "# What's the plan?" << endl;
cout << "\t >>1. Defend the village" << endl;
cout << "\t >>2. Get outta There!" << endl;
retry:
cout << "\nChoose 1 or 2: ";
cin >> choiceOne_Path;
if(choiceOne_Path == 1)
{
cout << "\n!!!----------------------Chapter One: Attack----------------------!!!" << endl;
cout << "\nChief: hey "<<name<<" get in formation!" << endl;
cout << "You: I don't have a weapon yet! " << endl;
cout << "# You run behind the chief." << endl;
cout << "#You run around the battlefield helpless and scared looking for a weapon." << endl;
}
elseif(choiceOne_Path == 2)
{
cout << "\n!!!----------------------Chapter One: Escape----------------------!!!" << endl;
cout << "\nYou: I'am outta here!" << endl;
cout << "Chief: where are ya going your'e gonna get killed out there!" << endl;
cout << "You: I know my way out." << endl;
cout << "Chief: Hey "<<name<<" come back!" << endl;
cout << "#You climb over a broken wall and run off towards the woods." << endl;
cout << "<--------------------------------> " << endl;
cout << "You manage to get out of there alive which is great except for the fact that you betrayed your village. " << endl;
}
else
{
cout << "You're doing it wrong! Press either '1' or '2', nothing else!" << endl;
goto retry;
}
cout << "\n----------------------Press any key to continue----------------------" << endl;
_getch();
system("cls");
switch(choiceOne_Path)
{
case 1: system("cls");
int choiceOne_Path;
cout << "# You find two corpses one with a bow and one with a sword" << endl;
cout << "\t >>1. Take the bow" << endl;
cout << "\t >>2. Take the sword" << endl;
cout << "\nChoose 1 or 2: ";
cin >> choiceOne_Path;
if(choiceOne_Path == 1)
{
cout << "\nChief: hey "<<name<<" I said get in formation!" << endl;
cout << "# you take aim and shoot" << endl;
cout << "#you managed to kill 3 locusts" <<endl;
}
elseif(choiceOne_Path == 2)
{
cout << "\nChief: hey "<<name<<" I said get in formation!" << endl;
cout << "# You charge into battle" << endl;
cout << "# you managed to kill 5 locusts before getting stabbed in the ribs" <<endl;
cout << "\nChief: hey "<<name<<" are you okay?"<<endl;
cout << "\nYou it's okay, it's just a flesh wound" <<endl;
}
else
{
cout << "You're doing it wrong! Press either '1' or '2', nothing else!" << endl;
goto retry;
}
break;
case 2: cout << "#You walk deeper into the forest (possibly lost)" << endl;
break;
}
system("cls");
cout << "#you keep walking until you come across a fork in the road" << endl;
cout << "\t >>1. go left" << endl;
cout << "\t >>2. go right" << endl;
cout << "\nChoose 1 or 2: ";
cin >> choiceOne_Path;
if(choiceOne_Path == 1)
{
cout << "# You keep walking on the left path until you see a" << endl;
cout << " dwarf village further up the road." << endl;
}
elseif(choiceOne_Path == 2)
{
cout << "# You keep walking on the left path until you realize that you" << endl;
cout << "are extremely thirsty and might collapse if you don't get something to drink" << endl;
}
else
{
cout << "You're doing it wrong! Press either '1' or '2', nothing else!" << endl;
goto retry;
}
return 0;
}
Are you sure you've posted the right code? I don't see the word "stumble" anywhere in your code.
Assuming you're talking about line 99 (which talks about a fork), that code isn't inside any kind of conditional block. It will always execute, because you haven't specified any conditions for it to choose whether or not to execute.
Some other comments:
- Don't use goto. It makes for messy, unstructured code, and it will be harder for you to follow the logic. C++ has perfectly good control structures, such as loops and functions, if you need to repeat parts of the code.
- It would make it much easier for you, and us, to follow the logic of your code, and spot any errors, if you used a consistent indentation style. Seriously, that will help you lots.
#include <iostream>
#include <conio.h>
#include <stdlib.h>
usingnamespace std;
void intro(); // all the lengthy introduction
void options(); // main menu options
void mainMenu(); // the main menu of the program. Has
void chapterOneAttack(); // every attack stuff in here
void chapterOneEscape(); // every escape stuffy here
void waitForKey(); // Program continues when use presses a key
char name[50]; // make this global since we need it just once and it will not change
// MAIN PROGRAM START
int main()
{
system("cls");
intro();
cout << "Okay before we can move on with the story," << endl;
cout << "I'am gonna have to kinda know your name, being the narrator and all." << endl;
cout << endl << endl << "Enter your name: ";
cin.getline(name, 50);
cout << " " << endl;
cout << "You better move fast, " << name << ". The locusts are attacking the city." << endl;
mainMenu();
return 0;
}// MAIN PROGRAM END
void intro()
{
cout << "I'am gonna have to kinda know your name, being the narrator and all.\n";
cout << "They're coming! Yells the scout." << endl;
cout << "Hold down the gates as long as you can!" << endl;
cout << "you see an army of white cross imperials" << endl;
cout << "(or locusts as they're called) advancing up the hill." << endl;
cout << " " << endl;
cout << "<---------------------------->" << endl;
cout << " " << endl;
}
void options()
{
cout << "# What's the plan?" << endl;
cout << "\t >>1. Defend the village" << endl;
cout << "\t >>2. Get outta There!" << endl;
cout << "\t >>0. Exit the program" << endl;
}
void mainMenu()
{
system("cls");
bool choosing = true;
while (choosing)
{
intro();
options();
cout << "\nChoose 1 or 2: ";
int choiceOne_Path;
cin >> choiceOne_Path;
switch (choiceOne_Path)
{
case 0:
choosing = false;
break;
case 1:
chapterOneAttack();
break;
case 2:
chapterOneEscape();
break;
default:
cout << "You're doing it wrong! Press either '1' or '2', nothing else!" << endl;
choosing = true;
break;
}
}
}
void chapterOneAttack()
{
system("cls");
cout << "\n!!!----------------------Chapter One: Attack----------------------!!!" << endl;
cout << "\nChief: hey " << name << " get in formation!" << endl;
cout << "You: I don't have a weapon yet! " << endl;
cout << "# You run behind the chief." << endl;
cout << "#You run around the battlefield helpless and scared looking for a weapon." << endl;
cout << "# You find two corpses one with a bow and one with a sword" << endl;
bool attacking = true;
while (attacking)
{
system("cls");
cout << "# You find two corpses one with a bow and one with a sword" << endl;
cout << "\t >>1. Take the bow" << endl;
cout << "\t >>2. Take the sword" << endl;
cout << "\t >>0. Go to main menu" << endl;
cout << "\nChoose 1 or 2 or 0: ";
int attackChoice;
cin >> attackChoice;
switch (attackChoice)
{
case 0:
attacking = false;
break;
case 1:
cout << "\nChief: hey " << name << " I said get in formation!" << endl;
cout << "# you take aim and shoot" << endl;
cout << "#you managed to kill 3 locusts" << endl;
attacking = true;
waitForKey();
break;
case 2:
cout << "\nChief: hey " << name << " I said get in formation!" << endl;
cout << "# You charge into battle" << endl;
cout << "# you managed to kill 5 locusts before getting stabbed in the ribs" << endl;
cout << "\nChief: hey " << name << " are you okay?" << endl;
cout << "\nYou it's okay, it's just a flesh wound" << endl;
attacking = true;
waitForKey();
break;
default:
cout << "You're doing it wrong! Press either '1' or '2', nothing else!" << endl;
attacking = true;
break;
}
}
}
void chapterOneEscape()
{
system("cls");
cout << "\n!!!----------------------Chapter One: Escape----------------------!!!" << endl;
cout << "\nYou: I'am outta here!" << endl;
cout << "Chief: where are ya going your'e gonna get killed out there!" << endl;
cout << "You: I know my way out." << endl;
cout << "Chief: Hey " << name << " come back!" << endl;
cout << "#You climb over a broken wall and run off towards the woods." << endl;
cout << "<--------------------------------> " << endl;
cout << "You manage to get out of there alive which is great except for the fact that you betrayed your village. " << endl;
bool escaping = true;
while (escaping)
{
system("cls");
cout << "#you keep walking until you come across a fork in the road" << endl;
cout << "\t >>1. go left" << endl;
cout << "\t >>2. go right" << endl;
cout << "\t >>0. Go to main menu" << endl;
cout << "\nChoose 1 or 2 or 0: ";
int escapeChoice = 0;
cin >> escapeChoice;
switch (escapeChoice)
{
case 0:
escaping = false;
break;
case 1:
cout << "# You keep walking on the left path until you see a" << endl;
cout << " dwarf village further up the road." << endl;
escaping = true;
waitForKey();
break;
case 2:
cout << "# You keep walking on the left path until you realize that you" << endl;
cout << "are extremely thirsty and might collapse if you don't get something to drink" << endl;
escaping = true;
waitForKey();
break;
default:
cout << "You're doing it wrong! Press either '1' or '2', nothing else!" << endl;
escaping = true;
}
}
}
void waitForKey()
{
cout << "\n\nPress any key to continue" << endl << endl;
_getch();
}