Not leaving the function

closed account (GbX36Up4)
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
#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

int gameloop();

void gameload();
void descgetter();
void input();
void output();
void locationgetter();
void NPC();
void itemobtained();

int location = 10000;
int north, east, south, west, npc, gender, npctalk, amount;
int coppercoin, silvercoin, goldcoin;
int x = 100;
int y = 100;

string name;
string desc;
string item;


int main(){ // where the code starts
	START: // come here if they enter a unacceptable answer
	string option; // string for their input
	cout << "Would you like to start a new game or load a previous one?" << endl;
	cout << "[N]ew Game" << endl;
	cout << "[L]oad Game" << endl;
	cin >> option; // get our answer
	if(option == "N" || "n" ) system("cls"); gameloop(); // if they choose to play a new one we go to the game loop
	if(option == "L" || "l" ) system("cls"); gameload(); // if they choose to load we go to the laod menu
	goto START; // otherwise we go back to the begging for a new answer
}

int gameloop(){ // here the game loops
	int gameon = 0; // int for the loop
	while(gameon == 0){ // while the gameon equals 0 we play the game
		system("cls"); // here we clear the screen
		locationgetter(); // get our location based of our x and y coords
		descgetter(); // get our desc based off our location 
		output(); // here we output the desc to the player
		input(); // now we get to see what the player has to say
	}
	return 0; // end the game when they exit the loop

}

void gameload(){ // here we can load a players previous game
}

void input(){ // here we get our players input
	int loop = 0; // the int for the loop
	while(loop == 0){ // while loop equals 0 we get input, we exit if they give us an answer
		string input; // this is the strign that will hold their input
		cin >> input; // now we get their input
			if(input == "west" && west == 0) cout << "You can not move that way." << endl; // if they choose west but they cant go west the loop restarts
			if(input == "north" && north == 0) cout << "You can not move that way." << endl; // if they choose north but they cant go north the loop restarts
			if(input == "east" && east == 0) cout << "You can not move that way." << endl; // if they choose east but they cant go east the loop restarts
			if(input == "south" && south == 0) cout << "You can not move that way." << endl; // if they choose south but they cant go south the loop restarts

			if(input == "west" && west == 1){--x; loop = 1;} // if they succesfully go west we take away from x
			if(input == "north" && north == 1){++y; loop = 1;} // if they succesfully go north we add to y
			if(input == "east" && east == 1) {++x; loop = 1;} // if they succesfully go east we add to x
			if(input == "south" && south == 1){--y; loop =1;} // if they succesfully go south we take away from y

			if(input == "talk" && npc == 0)cout << "There is no one to talk to." << endl; // if they choose to talk and no one is there this happens
			if(input == "talk" && npc == 1 && gender == 1)cout << "You move over to the man." << endl; NPC(); // if they choose to talk and it is a male this happens
			if(input == "talk" && npc == 1 && gender == 0)cout << "You move over to the woman." << endl;  // if they choose to talk and it is a female this happens

	}
}

void output(){ // here we output to the screen
	cout << desc << endl; // here we output the description
}

void itemobtained(){
	system("cls");
	system("color FE");
	system("color EF");
	system("color FE");
	system("color EF");
	system("color FE");
	system("color EF");
	system("color FE");
	system("color EF");
	system("color F6");
	cout << "You have obtained " << amount << " " << item << "(s)!!!" << endl;
	if(item == "gold coins"){goldcoin = goldcoin + amount;}
}

void NPC(){
	int answer1;

	if(npctalk == 1){ 
		cout << "Hey der, meh names John. If ya be lookin' for Wenheinster Town just go up north." << endl;
		cout << "\t[1] I don't have any money..." << endl;
		cout << "\t[2] Thanks for the help, i'll be on my way now." << endl;
		cin >> answer1;
			if(answer1 == 1){
				cout << "Well.. Here, take this, it a be 'nough for a weapon to defend youself." << endl;
				system("pause");
				item = "gold coins";
				amount = 10;
				itemobtained();
			}
	}
}

void locationgetter(){
	if(x == 100 && y == 100) location = 1;
	if(x == 99 && y == 101) location = 2;
	if(x == 100 && y == 101)location = 3;
}

void descgetter(){
	switch(location){
	case 1: desc = "You are in a large orange room. You can move north, east, south, and west. A man can be seen in the corner."; west = 0; south = 0; east = 0; north = 1; npc = 1; gender = 1; npctalk = 1; break;
	case 2: desc = "You are in a large blue room. You can move east."; west = 0; south = 0; east = 1; north = 0; npc = 0; gender = 2; npctalk = 0;  break;
	case 3: desc = "You are in a large grey room. You can move west and south."; west = 1; south = 1; east = 0; north = 0; npc = 0; gender = 2; npctalk = 0; break;
	}

}


When you tell John that you don't have any money and he gives you some it gets stuck in the function itemobtained(); I have tried all sorts of things and even tracked the program as to where it would go after it left that function and have n't found any problems.
I'm not sure exactly what is causing this particular problem, but you have several other problems:

in main:
 
if(option == "N" || "n" ) system("cls"); gameloop(); // if they choose to play a new one we go to the game loop 


"n" is always nonzero, and therefore always true. So this if statement will execute no matter what the user inputs. You probably meant to do this:

if(option == "N" || option == "n")

You have this same problem in a few other places.



Also, just sticking things on the same line as the if statement does not make them part of the if statement. If you have multiple commands in an if, you need to put curly braces around those statements. That same line in main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// doing this:
if(option == "N" || option == "n" ) system("cls"); gameloop();

// .. is the same as doing this:
if(option == "N" || option == "n" )   // if the user inputs n
    system("cls");  // clear the screen

gameloop();  // then run gameloop no matter what they input (this is not part of the if statement!)

// the correct way to do this would be with curly braces:
if(option == "N" || option == "n")
{ // put it in braces
    system("cls");
    gameloop();
}


You are doing this in several other places in your program.

Don't try to cram so much on one line. Adding new lines doesn't hurt anything.

Fix those and see if that fixes the problem.
closed account (GbX36Up4)
I put in curly braces and it still does not work.
It's not looping the function NPC. It's looping in this while loop:

while(loop == 0){ // while loop equals 0 we get input, we exit if they give us an answer

It is looping in this loop because there are some things you can type in that mean the value of loop does not change.

It calls the function NPC every time because you did not put NPC as part of the if statement's block of code.

if(input == "talk" && npc == 1 && gender == 1)cout << "You move over to the man." << endl; NPC();
See this? It's identical to this:
1
2
3
if(input == "talk" && npc == 1 && gender == 1)
{cout << "You move over to the man." << endl;}
 NPC(); 


See how the function NPC() is not part of the if block? That's why it gets run every time. As Disch says, use braces so you can see what you're doing. As a beginner, if you've got more than one semi-colon on a line, you're only going to confuse yourself.
Last edited on
closed account (GbX36Up4)
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
#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

int gameloop();

void gameload();
void descgetter();
void input();
void output();
void locationgetter();
void NPC();
void itemobtained();

int location = 10000;
int north, east, south, west, npc, gender, npctalk, amount;
int coppercoin, silvercoin, goldcoin;
int x = 100;
int y = 100;

string name;
string desc;
string item;


int main(){ // where the code starts
	START: // come here if they enter a unacceptable answer
	string option; // string for their input
	cout << "Would you like to start a new game or load a previous one?" << endl;
	cout << "[N]ew Game" << endl;
	cout << "[L]oad Game" << endl;
	cin >> option; // get our answer
	if(option == "N" || option == "n" ){ system("cls"); gameloop();} // if they choose to play a new one we go to the game loop
	if(option == "L" || option == "l" ){ system("cls"); gameload();} // if they choose to load we go to the laod menu
	goto START; // otherwise we go back to the begging for a new answer
}

int gameloop(){ // here the game loops
	int gameon = 0; // int for the loop
	while(gameon == 0){ // while the gameon equals 0 we play the game
		system("cls"); // here we clear the screen
		locationgetter(); // get our location based of our x and y coords
		descgetter(); // get our desc based off our location 
		output(); // here we output the desc to the player
		input(); // now we get to see what the player has to say
	}
	return 0; // end the game when they exit the loop

}

void gameload(){ // here we can load a players previous game
}

void input(){ // here we get our players input
	int loop = 0; // the int for the loop
	while(loop == 0){ // while loop equals 0 we get input, we exit if they give us an answer
		string input; // this is the strign that will hold their input
		cin >> input; // now we get their input
			if(input == "west" && west == 0) cout << "You can not move that way." << endl; // if they choose west but they cant go west the loop restarts
			if(input == "north" && north == 0) cout << "You can not move that way." << endl; // if they choose north but they cant go north the loop restarts
			if(input == "east" && east == 0) cout << "You can not move that way." << endl; // if they choose east but they cant go east the loop restarts
			if(input == "south" && south == 0) cout << "You can not move that way." << endl; // if they choose south but they cant go south the loop restarts

			if(input == "west" && west == 1){--x; loop = 1;} // if they succesfully go west we take away from x
			if(input == "north" && north == 1){++y; loop = 1;} // if they succesfully go north we add to y
			if(input == "east" && east == 1) {++x; loop = 1;} // if they succesfully go east we add to x
			if(input == "south" && south == 1){--y; loop =1;} // if they succesfully go south we take away from y

			if(input == "talk" && npc == 0){cout << "There is no one to talk to." << endl;} // if they choose to talk and no one is there this happens
			if(input == "talk" && npc == 1 && gender == 1){cout << "You move over to the man." << endl; NPC();} // if they choose to talk and it is a male this happens
			if(input == "talk" && npc == 1 && gender == 0){cout << "You move over to the woman." << endl;}  // if they choose to talk and it is a female this happens

	}
}

void output(){ // here we output to the screen
	cout << desc << endl; // here we output the description
}

void itemobtained(){
	system("cls");
	system("color FE");
	system("color EF");
	system("color FE");
	system("color EF");
	system("color FE");
	system("color EF");
	system("color FE");
	system("color EF");
	system("color F6");
	cout << "You have obtained " << amount << " " << item << "(s)!!!" << endl;
	if(item == "gold coins"){goldcoin = goldcoin + amount;}
}

void NPC(){
	int answer1;

	if(npctalk == 1){ 
		cout << "Hey der, meh names John. If ya be lookin' for Wenheinster Town just go up north." << endl;
		cout << "\t[1] I don't have any money..." << endl;
		cout << "\t[2] Thanks for the help, i'll be on my way now." << endl;
		cin >> answer1;
			if(answer1 == 1){
				cout << "Well.. Here, take this, it a be 'nough for a weapon to defend youself." << endl;
				system("pause");
				item = "gold coins";
				amount = 10;
				itemobtained();
			}
	}
}

void locationgetter(){
	if(x == 100 && y == 100) location = 1;
	if(x == 99 && y == 101) location = 2;
	if(x == 100 && y == 101)location = 3;
}

void descgetter(){
	switch(location){
	case 1: desc = "You are in a large orange room. You can move north, east, south, and west. A man can be seen in the corner."; west = 0; south = 0; east = 0; north = 1; npc = 1; gender = 1; npctalk = 1; break;
	case 2: desc = "You are in a large blue room. You can move east."; west = 0; south = 0; east = 1; north = 0; npc = 0; gender = 2; npctalk = 0;  break;
	case 3: desc = "You are in a large grey room. You can move west and south."; west = 1; south = 1; east = 0; north = 0; npc = 0; gender = 2; npctalk = 0; break;
	}

}


I added in the curly brace at that spot and it still does not work.
It does work. It does exactly what you programmed it to do.

We are not psychic. We are not magicians. If you say "It doesn't work" the answer we give will be "fix it".

If you want more than that, you will have to tell us what it does and what you want it to do. Right now, it loops endlessly in the while (loop == 0) loop, changing occasionally when you move room.

Here is some sample output:
Would you like to start a new game or load a previous one?
[N]ew Game
[L]oad Game
N
You are in a large orange room. You can move north, east, south, and west. A man
 can be seen in the corner.
north
You are in a large grey room. You can move west and south.
west
You are in a large blue room. You can move east.
east
You are in a large grey room. You can move west and south.
talk
There is no one to talk to.
west
You are in a large blue room. You can move east.
talk
There is no one to talk to.
east
You are in a large grey room. You can move west and south.
south
You are in a large orange room. You can move north, east, south, and west. A man
 can be seen in the corner.
talk
You move over to the man.
Hey der, meh names John. If ya be lookin' for Wenheinster Town just go up north.

        [1] I don't have any money...
        [2] Thanks for the help, i'll be on my way now.
1
Well.. Here, take this, it a be 'nough for a weapon to defend youself.
You have obtained 10 gold coins(s)!!!
talk
You move over to the man.
Hey der, meh names John. If ya be lookin' for Wenheinster Town just go up north.

        [1] I don't have any money...
        [2] Thanks for the help, i'll be on my way now.
2
north
You are in a large grey room. You can move west and south.


Get rid of all that system("cls") junk and maybe you'll be able to see what's actually going on.

And please, please, stop putting as much as you can on one line. You are not being charged by the line.
Last edited on
Topic archived. No new replies allowed.