I don't know what's wrong with my code

Write your question here.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace 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;
    }
    else if(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;
    }
    else if(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;
    }
    else if(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;
}
Last edited on
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.
Sorry I ment "come across" when I said stumble.

I'am also sorry for such a basic question but I thought it was in a conditional block, that's why I couldn't find what was wrong with it.

also not meaning to be rude how would a loop or function help with this type of program, would it make it easier.
Last edited on
@IIGabrielII, this is how the program can look like with functions and C++ loops
Works with VS2017 Enterprice

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <iostream>
#include <conio.h> 
#include <stdlib.h>
using namespace 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();
}
			
Last edited on
Topic archived. No new replies allowed.