Dungeon Program With Questions

Pages: 12
Thank you for elaborating on that Anon :) Makes sense. Good to keep in mind next time I encounter it!
I have an issue where my declaration of the dynamic array of type Mob, breaks my code. It makes it unable to move the player. Memory issue that I've never encountered... Somehow the invalid way of declaring mob is overwriting player? Or something like that? I am tempted to use a vector however I have no reason to add or remove from the array. It may be easier to manage a complicated dynamic array with a vector?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

	Mob * Monster;
	cout << "Enter # of Monsters you would like to face. 0-4 \n";
	cin >> NumOfMobs;
	while (NumOfMobs < 0 || NumOfMobs > 4)
	{
		cout << "Invalid Input, Please input 1-4 \n";
		cin >> NumOfMobs;
	}

	Monster = new Mob[NumOfMobs]();//Declares dynamic array Monster of type mob size UserInput
//I added below in an attempt to make it initialize properly, Not sure this is even correct
	for (int i = 0; i < NumOfMobs; i++)
	{
		Monster[i] = Mob();
	}


Full Main statement
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
int main()
{
	srand(time(NULL));//Seed random 
	int TrapCount = 3;
	int NumOfMobs;
	Game Game;
	Player Player;
	Mob * Monster;
	cout << "Enter # of Monsters you would like to face. 0-4 \n";
	cin >> NumOfMobs;
	while (NumOfMobs < 0 || NumOfMobs > 4)
	{
		cout << "Invalid Input, Please input 1-4 \n";
		cin >> NumOfMobs;
	}

	Monster = new Mob[NumOfMobs]();//Declares dynamic array Monster of type mob size UserInput
	for (int i = 0; i < NumOfMobs; i++)
	{
		Monster[i] = Mob();
	}



	Game.InitializeBoard();

	Game.PlaceGameObjects(TrapCount);

	Game.PlacePlayer(Player);
	//TODO: Ask how many mobs to be placed
	Game.PlaceMob(Monster,NumOfMobs);

	do
	{
		Game.PrintBoard();

		Game.MovePlayer(Player);

		Game.MoveMob(Monster, NumOfMobs);//Allows mob to move into player... Maybe bad.        

	} while (Game.CheckGameState(Player) == true);

	Game.PrintBoard(); //Prints final move where you won/lost
	system("pause");
	return 0;
}


The Mob functions run well, adds mobs and moves them all independently. Just no more player movement(at all) It wont even detect that the player has tried to move towards a boundary. If I omit all mob related code in main it runs smoothly, back to player being able to move.
Last edited on
Apparently its cin breaking my code. I changed it to a vector and the only issue I have is that if i use cin in any form or fashion, even as an initializer for a number I dont even use in the code. Then I can no longer move player. Im lost.
Solved. Getchar in moveplayer function was grabbing new line so i was skipping the entire move.
Just switched it to cin. Was originally wanting to get input without pressing enter but couldn't get that to work properly.
Didn't realize the newline was stored throughout whole program even using cin.clear()

DONE. (Though with vector I can add a lot more interesting things...)

edit: cin.clear() only resets flags. Needed to flush buffer. By replacing getchar with cin it now flushes the buffer before accepting the new input
Last edited on
Topic archived. No new replies allowed.
Pages: 12