Program inproperly closing

My program keeps closing on me during this function of my code. I thought it might have my switch/case statement so I replaced it with if/else if/else statements and it still closes. If you guys could help me out I'd appreciate it.

I haven't seen it yet with the if statements but with the switch/case sometimes when I input like 'S' it would then respond with an action as if I input 'N' or something that was not 'S'.

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
#include <iostream>
#include <string>
#include "Player.h"

using namespace std;

void showStats(Player&);
void saveGame(Player&);
void loadGame(Player&);
void exitGame(Player&);
void showMenu(Player&);

void moveLocation(Player& User)
{

	bool safeDirection, findCreature, setCoords = false;
	int xCord = 0, yCord = 0;
	int rndmCreature;
	char direction;

	do{
		do{
		rndmCreature = (rand()% 3);
		findCreature = false;
		safeDirection = true;
			

		if (setCoords == false)
		{
			User.setXCord(User.getXCord());
			User.setYCord(User.getYCord());
		}
		setCoords = true;

		
		cout << "\n(M)enu..." << endl;
		cout << "Where would you like to go? (N)orth, (E)ast, (S)outh, (W)est: ";
		cin >> direction;

		direction = (toupper(direction));
		if (direction == 'N')
		{
			if (User.getXCord() > 0)
			{
				xCord = (xCord--);
				User.setXCord(xCord);
				
				if (rndmCreature == 1)
					findCreature = true;
				break;
			}
			else
			{
				cout << "You can not go further North.\n" << endl;
				safeDirection = false;
				break;
			}
		}
		else if (direction == 'E')
		{
			if (User.getYCord() < 19)
			{
				yCord = (yCord++);
				User.setYCord(yCord);

				if (rndmCreature == 1)
					findCreature = true;
				break;
			}
			else
			{
				cout << "You can not go further East.\n" << endl;
				safeDirection = false;
				break;
			}
		}
		else if (direction == 'S')
		{
			if (User.getXCord() < 19)
			{
				xCord = (xCord++);
				User.setXCord(xCord);

				if (rndmCreature == 1)
					findCreature = true;
				break;
			}
			else
			{
				cout << "You can not go further South.\n" << endl;
				safeDirection = false;
				break;
			}
		}
		else if (direction == 'W')
		{
			if (User.getYCord() > 0)
			{
				yCord = (yCord--);
				User.setYCord(yCord);
				
				if (rndmCreature == 1)
					findCreature = true;
				break;
			}
			else
			{
				cout << "You can not go further West.\n" << endl;
				safeDirection = false;
				break;
			}
		}
		else if (direction == 'M')
			showMenu(User);
		else
		{
			cout << "Invalid input!\n" << endl;
			safeDirection = false;
		}
		
		
		}while (safeDirection == false);
	}while (findCreature == false);
}
closed account (S6k9GNh0)
This might just be me but, I'm assuming that something is breaking the do - while loop (which I'm confused as to why your not just using a while loop althought it shouldn't matter). Make sure that findCreature and safeDirection are false when the loop is about to iterate.
Last edited on
well even when the loop breaks the program should go back to the function that called this one. and not close down the whole program...
Last edited on
It's hard to say without being able to run the code. Try putting somebreak points in your code. If you don't know what those are or don't feel like learning about them just now, put some output statements in different parts of your code to see where the program is getting to (and where it's not getting to.

For example, if you have code like:

1
2
3
cerr << "checkpoint 1" << endl;
my_crazy_function();
cerr << "checkpoint 2" << endl;


And you never see "checkpoint2" printed to the console, you know to look at my_crazy_function() a bit closer.

Give that a shot and report back if you still have issues.

Also, as far as 'S' acting like a non-'S', output the user entry just as a sanity check. Right before you begin testing the input, print it to the console to make sure it's what you expect. cin might act funny if you accidentally type in 'NS' instead of 'S', for example.
Alright I'm still not sure why it's doing this but I put in the checks and commented some unnecessary stuff out.

I put in a check after the cin two in each "north, east, south, west, two at the end of the do/while loop, a cin.get() and one after the do/while loop.

If I input a 'N', 'E', 'W', or 'S', my progrom doesn't show either of "check end1" or "check end2" nor does it do the cin.get();

*edit: Figured out the break; was making the "check end"'s not happen. as to why the program is closing i still don't know.

If I input a letter other than those though it does perform both of the "check end" and it does the cin.get(); and on the second time through the loop the program will close regardless of what I input.

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
#include <iostream>
#include <string>
#include "Player.h"

using namespace std;

void showMenu(Player&);

void moveLocation(Player& User)
{

	bool safeDirection, findCreature, setCoords = false;
	int xCord = 0, yCord = 0;
	int rndmCreature;
	char direction;

	do{
		rndmCreature = (rand()% 3);
		findCreature = false;
		safeDirection = true;
			

		/*if (setCoords == false)
		{
			User.setXCord(User.getXCord());
			User.setYCord(User.getYCord());
		}
		setCoords = true; */

		
		cout << "\n(M)enu..." << endl;
		cout << "Where would you like to go? (N)orth, (E)ast, (S)outh, (W)est: ";
		cin >> direction;
		cin.ignore(100, '\n');
cout << "check cin" << endl;
		direction = (toupper(direction));
		if (direction == 'N')
		{
			cout << "check1n" << endl;
			//if (User.getXCord() > 0)
			//{
				//xCord = (xCord--);
				//User.setXCord(xCord);
				
				if (rndmCreature == 1)
					findCreature = true;
				cout << "check2n" << endl;
				break;
			//}
			/*else
			{
				cout << "You can not go further North.\n" << endl;
				safeDirection = false;
				break;
			}*/
		}
		else if (direction == 'E')
		{
			cout << "check1e" << endl;
			//if (User.getYCord() < 19)
			//{
				//yCord = (yCord++);
				//User.setYCord(yCord);

				if (rndmCreature == 1)
					findCreature = true;
				cout << "check2e" << endl;
				break;
			//}
			/*else
			{
				cout << "You can not go further East.\n" << endl;
				safeDirection = false;
				break;
			}*/
		}
		else if (direction == 'S')
		{
			cout << "check1s" << endl;
			//if (User.getXCord() < 19)
			//{
				//xCord = (xCord++);
				//User.setXCord(xCord);

				if (rndmCreature == 1)
					findCreature = true;
				cout << "check2s" << endl;
				break;
			//}
			/*else
			{
				cout << "You can not go further South.\n" << endl;
				safeDirection = false;
				break;
			}*/
		}
		else if (direction == 'W')
		{
			cout << "check1w" << endl;
			//if (User.getYCord() > 0)
			//{
				//yCord = (yCord--);
				//User.setYCord(yCord);
				
				if (rndmCreature == 1)
					findCreature = true;
				cout << "check2w" << endl;
				break;
			//}
			/*else
			{
				cout << "You can not go further West.\n" << endl;
				safeDirection = false;
				break;
			}*/
		}
		/*else if (direction == 'M')
			showMenu(User);
		else
		{

			cout << "Invalid input!\n" << endl;
			safeDirection = false;
		}*/
		
		cout << "check end1" << endl;
		cout << "check end2" << endl;
		cin.get();
	}while ((safeDirection == false) && (findCreature == false));
	cout << "check end while" << endl;
}
Last edited on
Topic archived. No new replies allowed.