Hi,

I have built a text based adventure game.

The last thing I have to do (this is for an assignment), is ask the user if they want to play again.

So, I thought the code below would probably be the best way to do it, seeing as I don't need any data from the game they just played, i'd delete the game altogether

bool play = true;
		char c;
				
		while(play)
		{
			AdventureGame* g = new AdventureGame(fileName, widthAndHeight);
			g->Run();
			
			delete g;

			system("stty raw -echo");
			cin.get(c);					
			system("stty -raw echo");

			if(c!='y')
			{
				break;
			}
			else
			{
				cout << 'y' << endl;
				play=true;
			}
		}

But it's not working.

When the user presses 'y' to play again, the last screen from the last game is displayed, and then the user is instantly asked if they want to play.

What am I doing wrong here?

PS... the game itself and all other code works fine.

Thanks for your help,

Jim.

Hi,

Not sure, but you are deleting object inside the while loop, so when loop runs again, it might throw an exception as g is gone ! not sure though !

vsha041

Is your AdventureGame class using static variables? If so the new instance could be using some state information from the previous run. Check that these variables are properly initialised in the constructor, or convert them to non-static members if possible.
The easiest way to get to the bottom of it is just to step through it with a debugger. Have you tried that?

commented: Nice :) ! +1

Is your AdventureGame class using static variables? If so the new instance could be using some state information from the previous run. Check that these variables are properly initialised in the constructor, or convert them to non-static members if possible.
The easiest way to get to the bottom of it is just to step through it with a debugger. Have you tried that?

YAHOO! Thanks MrSpigot. I owe you one ;)

Within the AdventureGame class I had pointers to objects from other classes. I was creating those objects and then using a static variable to access them in other member functions. So when the game object was deleted and the created again, it was using the same objects as the first run.

Cheers,

Jim.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.