Hello everyone;
I'm trying to do something which I'm not sure if it is available in c++. Actually, I'm developing a game with functions.And to reset the game when the user presses a button or a certain event occurs, the program must start running from all over, say from the very first line of the code. Is it possible to go back some number of lines while keeping the global constants,func.declarations intact, or simply start running from the scratch? Although I'm developing a game, I posted this thread here in c++, because I thought it was more relevant to c++ then games development.
Thank you for your valuable answers.
Best regards.
Recommended Answers
Jump to PostPut your code in an infinite while loop (using
while(true)
) andbreak
out of it when you want to exit. All of your function declarations would stand anyway and global variables would persist during the program execution, so I'm not sure what you mean by that.
Jump to PostCalling main results in undefined behaviour, so not really.
Jump to Post@OP: You should do something like this :
int main(){ Game game; while(game.isNotOver()){ int state = game.getState(); switch(state){ case PAUSE_GAME : pauseGame(game); break; case PLAY_GAME : playGame(game); break; case EXIT_GAME : cleanUp(game); break; } if(!game.isNotOver){ //if game is over bool playAgain = checkIfUserWantsToPlayAgain(); if(playAgain) game = Game(); …
All 11 Replies
jonsca
1,059
Quantitative Phrenologist
Team Colleague
Featured Poster
mike_2000_17
2,669
21st Century Viking
Team Colleague
Featured Poster
AuburnMathTutor
37
Junior Poster
Aranarth
126
Posting Whiz in Training
jonsca
commented:
Agreed
+4
ssmg
0
Newbie Poster
Aranarth
126
Posting Whiz in Training
AuburnMathTutor
37
Junior Poster
Aranarth
126
Posting Whiz in Training
AuburnMathTutor
37
Junior Poster
mrnutty
761
Senior Poster
jonsca
commented:
I like it.
+4
ssmg
0
Newbie Poster
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.