14 Replies - 1790 Views - Last Post: 03 January 2008 - 12:06 PM Rate Topic: -----

#1 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Need a program to restart from a certain point in the code!

Posted 02 January 2008 - 02:02 AM

Im making a game, again. Something simple compared to what I have attempted :)

My problem is i created a menu, using switch.

If the player chooses how-to they read the tutorial and then the game ends.
I want it to either return to the menu and restart the program from that point in code, how do i do this?

If thats not very clear maybe my code will help.:

 

/* GRID WARS */  

/* 

  Development Source Only! Not Release Source
  
  Development Start Date: 
  
  Tuesday 1/1/2008
  
*/ 

// Headers

#include <iostream> 
#include <cstring>
#include <stdlib.h>

using namespace std;

int main()
{  

	// Variables
	
	int ins_loop;
	
	
	// Menu 
	cout << "*==Main Menu==* \n";
	cout << "|			 | \n";
	cout << "|1. Play	  | \n";
	cout << "|			 | \n";
	cout << "|2. How-to	| \n";
	cout << "|			 | \n";
	cout << "|3. Quit	  | \n";
	cout << "|			 | \n";
	cout << "*=============* \n";
	cin  >> m_menu;
	cin.ignore();
	system ("cls");	
		 
		 // Menu Control 
		 
		 switch ( m_menu ){ 
		 
		 
		 case 1:{ 
		 
		   // The Game 
		   
				}
		 
		 case 2:{ 
		 
		   
		   // Instructions
		   cout << "GRID WARS MANUEL								  \n";
		   
		   cout << "Your Island Is Under Attack!					  \n";
		   cout << "Destroy the enemy fleet by entering the grid	  \n"; 
		   cout << "coordinates to call in an air strike.			 \n"; 

		   cout << "The grid or ' Battle Field ' will loo like this:  \n";
		   cout << "												  \n";
		   cout << "	1	 2	  3								\n";
		   cout << "A [   ] [   ]  [   ]							  \n";
		   cout << "B [   ] [ X ]  [   ]							  \n"; 
		   cout << "C [   ] [   ]  [   ]							  \n";
		   cout << "												  \n;"
		   cout << "So to attack the 'X' you would enter:			 \n"; 
		   cout << "												  \n";
		   cout << "2 B											   \n";

cout << "Make sure to put a space between the coordinates.			\n";

cout << "However, the grid ' Battle Field ' will be much larger.	  \n";

cout << "10 x 11 to be exact.										 \n"; 

cout << "The Example was only 3 x 3.								  \n";

cout << "Enemy Units and Values:									  \n"; 
cout << " \n";
cout << "<==> Battle Ship	   | 20 points						   \n";						 
cout << "<>	 Attack Boat	 | 1  points						   \n";						  
cout << "(==)   Submarine	   | 30 points						   \n";						 
cout << "[==]   Aircraft Carrier| 50 points						   \n";						 
cout << "(:::)	Civilian Ship | -10 Health Points to island		 \n";
cout << "															 \n";
cout << "															 \n";
cout << "DO NOT ATTACK CIVILIANS!									 \n"; 
cin.get();						

		   
		   
		   
				}
		 
		 case 3:{ 
		 
		   exit(1); 
				}
		 
			default: { 
			
			// Error Notice
			cout << "**ERROR**ERROR**\n";
			cout << "*ILLEGAL CHOICE*\n";
			cout << "**ERROR**ERROR**\n";
			cin.get(); 
			
			// Exit With Error
			exit(1);
					 }



I want the program to, after the player is done reading the manuel, to restart at the cout-based menu.
Any ideas? thanks! :P

Is This A Good Question/Topic? 0
  • +

Replies To: Need a program to restart from a certain point in the code!

#2 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Need a program to restart from a certain point in the code!

Posted 02 January 2008 - 02:33 AM

There are a couple of ways to do this, but the simplest (in terms of fewest mods to the current code) would be to place the entire menu + the switch-case structure into a do-while loop. The continuation condition for the loop could be that the choice variable (m_menu) is not equal to 3:
while (m_menu != 3) {
	//menu code goes here
	
	//switch-case structure goes here
}



So, if the user were to select the instructions in your code, the instructions would execute, then the program would loop back to the menu. Same for the gameplay (though you could put a user input step in at the end of the game asking whether the user wants to continure). But if the user selects 3 (the quit condition), the loop would break and the program would end - you could also remove the exit() statement in the third menu choice, in this case.

Hope that helps. Try it out, and get back to us if you have any more questions or need clarification. :)
Was This Post Helpful? 0
  • +
  • -

#3 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Need a program to restart from a certain point in the code!

Posted 02 January 2008 - 05:18 AM

Odd, when i do this:


/* GRID WARS */  

/* 

  Development Source Only! Not Release Source
  
  Development Start Date: 
  
  Tuesday 1/1/2008
  
*/ 

// Headers

#include <iostream> 
#include <cstring>
#include <stdlib.h>

using namespace std;

int main()
{  

	// Variables
	
	int m_menu;
	
	
 while (m_menu != 3) {
	
	// Menu 
	cout << "*==Main Menu==* \n";
	cout << "|			 | \n";
	cout << "|1. Play	  | \n";
	cout << "|			 | \n";
	cout << "|2. How-to	| \n";
	cout << "|			 | \n";
	cout << "|3. Quit	  | \n";
	cout << "|			 | \n";
	cout << "*=============* \n";
	cin  >> m_menu;
	cin.ignore();
	system ("cls");	
		 
		 // Menu Control 
		 
		 switch ( m_menu ){ 
		 
		 
		 case 1:{ 
		 
		   // The Game 
		   
				}
		 
		 case 2:{ 
		 
		   
		   // Instructions
		   cout << "GRID WARS MANUEL								  \n";
		   
		   cout << "Your Island Is Under Attack!					  \n";
		   cout << "Destroy the enemy fleet by entering the grid	  \n"; 
		   cout << "coordinates to call in an air strike.			 \n"; 

		   cout << "The grid or ' Battle Field ' will loo like this:  \n";
		   cout << "												  \n";
		   cout << "	1	 2	  3								\n";
		   cout << "A [   ] [   ]  [   ]							  \n";
		   cout << "B [   ] [ X ]  [   ]							  \n"; 
		   cout << "C [   ] [   ]  [   ]							  \n";
		   cout << "												  \n";
		   cout << "So to attack the 'X' you would enter:			 \n"; 
		   cout << "												  \n";
		   cout << "2 B											   \n";

cout << "Make sure to put a space between the coordinates.			\n";

cout << "However, the grid ' Battle Field ' will be much larger.	  \n";

cout << "10 x 11 to be exact.										 \n"; 

cout << "The Example was only 3 x 3.								  \n";

cout << "Enemy Units and Values:									  \n"; 
cout << " \n";
cout << "<==> Battle Ship	   | 20 points						   \n";						 
cout << "<>	 Attack Boat	 | 1  points						   \n";						  
cout << "(==)   Submarine	   | 30 points						   \n";						 
cout << "[==]   Aircraft Carrier| 50 points						   \n";						 
cout << "(:::)	Civilian Ship | -10 Health Points to island		 \n";
cout << "															 \n";
cout << "															 \n";
cout << "DO NOT ATTACK CIVILIANS!									 \n"; 
cin.get();						

		   
		   
		   
				}
		 
		 case 3:{ 
		   cin.get();
		   exit(1); 
				}
		 
			default: { 
			
			// Error Notice
			cout << "**ERROR**ERROR**\n";
			cout << "*ILLEGAL CHOICE*\n";
			cout << "**ERROR**ERROR**\n";
			cin.get(); 
			
			// Exit With Error
			exit(1);
					 }
					  }
					   } 
						} 



When I select 2, read and press enter twice the program terminates ? :(

This post has been edited by Delta_Echo: 02 January 2008 - 05:23 AM

Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Need a program to restart from a certain point in the code!

Posted 02 January 2008 - 06:02 AM

You're not asking the user for input again...in order for the while condition to be tested, you'll need to re prompt the user for input after the action takes place.
while (m_menu != 3) {
	//menu code goes here
	
	//switch-case structure goes here
   cout<<"Enter Choice:"<<endl;
   cin>>m_menu;
}


Was This Post Helpful? 0
  • +
  • -

#5 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Need a program to restart from a certain point in the code!

Posted 02 January 2008 - 06:15 AM

Ok, i tried it like this:

 

/* GRID WARS */  

/* 

  Development Source Only! Not Release Source
  
  Development Start Date: 
  
  Tuesday 1/1/2008
  
*/ 

// Headers

#include <iostream> 
#include <cstring>
#include <stdlib.h>

using namespace std;

int main()
{  

	// Variables
	int m_menu;
	
	while ( m_menu != 3 ) {	
	
	// Menu 
	cout << "*==Main Menu==* \n";
	cout << "|			 | \n";
	cout << "|1. Play	  | \n";
	cout << "|			 | \n";
	cout << "|2. How-to	| \n";
	cout << "|			 | \n";
	cout << "|3. Quit	  | \n";
	cout << "|			 | \n";
	cout << "*=============* \n";
	cin  >> m_menu;
	cin.ignore();
	system ("cls");	
		 
		 // Menu Control 
		 
		 switch ( m_menu ){ 
		 
		 
		 case 1:{ 
		 
		   // The Game 
		   
				}
		 
		 case 2:{ 
		 
		   
		   // Instructions
		   cout << "GRID WARS MANUEL								  \n";
		   
		   cout << "Your Island Is Under Attack!					  \n";
		   cout << "Destroy the enemy fleet by entering the grid	  \n"; 
		   cout << "coordinates to call in an air strike.			 \n"; 

		   cout << "The grid or ' Battle Field ' will loo like this:  \n";
		   cout << "												  \n";
		   cout << "	1	 2	  3								\n";
		   cout << "A [   ] [   ]  [   ]							  \n";
		   cout << "B [   ] [ X ]  [   ]							  \n"; 
		   cout << "C [   ] [   ]  [   ]							  \n";
		   cout << "												  \n";
		   cout << "So to attack the 'X' you would enter:			 \n"; 
		   cout << "												  \n";
		   cout << "2 B											   \n";

cout << "Make sure to put a space between the coordinates.			\n";

cout << "However, the grid ' Battle Field ' will be much larger.	  \n";

cout << "10 x 11 to be exact.										 \n"; 

cout << "The Example was only 3 x 3.								  \n";

cout << "Enemy Units and Values:									  \n"; 
cout << " \n";
cout << "<==> Battle Ship	   | 20 points						   \n";						 
cout << "<>	 Attack Boat	 | 1  points						   \n";						  
cout << "(==)   Submarine	   | 30 points						   \n";						 
cout << "[==]   Aircraft Carrier| 50 points						   \n";						 
cout << "(:::)	Civilian Ship | -10 Health Points to island		 \n";
cout << "															 \n";
cout << "															 \n";
cout << "DO NOT ATTACK CIVILIANS!									 \n"; 
cin.get();						
					}   
		   
		 case 3:{ 
		 
		   exit(1); 
				}
		 
			default: { 
			
			// Error Notice
			cout << "**ERROR**ERROR**\n";
			cout << "*ILLEGAL CHOICE*\n";
			cout << "**ERROR**ERROR**\n";
			cin.get(); 
			
			// Exit With Error
			exit(1);
					 }
	cout << "*==Main Menu==* \n";
	cout << "|			 | \n";
	cout << "|1. Play	  | \n";
	cout << "|			 | \n";
	cout << "|2. How-to	| \n";
	cout << "|			 | \n";
	cout << "|3. Quit	  | \n";
	cout << "|			 | \n";
	cout << "*=============* \n";
	cin  >> m_menu;
	cin.ignore();
		
	 }
	  }
	   }
		



still doesn't work?
Was This Post Helpful? 0
  • +
  • -

#6 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: Need a program to restart from a certain point in the code!

Posted 02 January 2008 - 01:25 PM

You could use goto statements.

Example:
start: // This is a label. Notice the ":" at the end, it's not a ";".

do_something();
do_something_else();

goto start; // This will take you back to start.



It's worth noting however that using loops is the proper way, and you should avoid using goto's whenever possible. :)
Was This Post Helpful? 0
  • +
  • -

#7 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Need a program to restart from a certain point in the code!

Posted 02 January 2008 - 01:28 PM

I think that the problem is that you don't have any break statements in your cases. Switch-case statements have a "fall-through" behavior in C/C++, so unless you terminate each case with a break, the code will continue to execute all of the code following the initial case label. So include those breaks to prevent the code from running right through to the exit condition. An example using the first few lines of your switch-case would be:
		switch (m_menu) {
			
			case 1: {
				
				// The Game 
				break;//<--  IMPORTANT - last statement in each case!
			}
			case 2: {...


The fall-though behavior can be convenient sometimes, but if you don't know about it already, it can get pretty frustrating.
Was This Post Helpful? 0
  • +
  • -

#8 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Need a program to restart from a certain point in the code!

Posted 02 January 2008 - 01:34 PM

View PostTom9729, on 2 Jan, 2008 - 03:25 PM, said:

You could use goto statements.

Example:
start: // This is a label. Notice the ":" at the end, it's not a ";".

do_something();
do_something_else();

goto start; // This will take you back to start.



It's worth noting however that using loops is the proper way, and you should avoid using goto's whenever possible. :)

eww goto and labels are one of few things that should be avoided like the plague.
Was This Post Helpful? 0
  • +
  • -

#9 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Need a program to restart from a certain point in the code!

Posted 02 January 2008 - 10:05 PM

So, why is GOTO so evil? lol

I got the code to work, just added break; :) thanks!
Now to write the game.
Was This Post Helpful? 0
  • +
  • -

#10 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Need a program to restart from a certain point in the code!

Posted 02 January 2008 - 11:54 PM

goto isn't necessarily evil, it's just sort of evil in C programming...and even more so in C++. It's a throwback to fundamentally sequential languages like BASIC, that didn't have the syntax handling required for procedural or object oriented programming.

In C, you can accomplish the same type of behavior more safely, more modularly, and with greater readability using functions to break the code down into discrete blocks of related code. So if you had a goto xyz type of statement, you would instead call the appropriate function at that point. This makes your code much easier to follow, improves the optimizations that the compiler can make, and reduces the chance of making a major error either directing your program control to the wrong point in the code, or including inappropriate code within the statements that will execute following a goto jump. It also makes debugging your code a much easier task.

And in C++, you're better off using more of an object oriented approach - you could have, for example, a menu object that controls the flow of the program by directing to a game object, or even different game objects if you have multiple play modes. Then, when you move on to your next project, you've already got a nice refined menu class that you can include with minimal modification, boosting your development speed (at least in theory).

The sequential approach to programming is a pretty outdated concept, at least for applications like what you've got here. The linearity of execution can be an easy way to think about programming initially, but things get easier when one adopts a more procedural or object-oriented approach.

But of course, it's a learning curve. The next stage in learning will start when you finish the game and think to yourself "how could I have done that better?", or when you get stuck and can't figure out how to go on to the next stage. Then it'll be time to learn a new way of doing something - that's how a lot of programmers learn.




...of course, you don't actually have any goto statements in here, so that part of the discussion is moot. But you may want to try breaking this up into functions (e.g. one for a menu, one for the gameplay) as a next step.
Was This Post Helpful? 0
  • +
  • -

#11 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Need a program to restart from a certain point in the code!

Posted 03 January 2008 - 04:11 AM

Cool, know of any good tutorials on making a function?
Thanks!
Was This Post Helpful? 0
  • +
  • -

#12 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Need a program to restart from a certain point in the code!

Posted 03 January 2008 - 04:38 AM

The C++ tutorials section right here at </dic> has one: http://www.dreaminco...wtopic27004.htm :)

http://www.cplusplus.../functions.html and its follow-up are pretty good as well. The demo functions are all quite short, but they demonstrate the concepts well.
Was This Post Helpful? 0
  • +
  • -

#13 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Need a program to restart from a certain point in the code!

Posted 03 January 2008 - 04:39 AM

Thanks! :)
Was This Post Helpful? 0
  • +
  • -

#14 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: Need a program to restart from a certain point in the code!

Posted 03 January 2008 - 06:38 AM

View PostDelta_Echo, on 3 Jan, 2008 - 05:05 AM, said:

So, why is GOTO so evil? lol

I got the code to work, just added break; :) thanks!
Now to write the game.

goto is evil when used in place of a loop, because it encourages so-called "spaghetti code", which ignores scope rules and allows subtle bugs to creep in unawares, when 99% of the time, there's a far better, safer, and more readable alternative.

there are always exceptional cases where goto is sometimes considered to be 'ok' by some, but these situations are few and far between IMHO, so the best advice generally given to beginners is to use another construct.


Here's an interesting discussion involving Linus Torvalds on the use of goto in Linux Kernel code http://kerneltrap.org/node/553/2131
Was This Post Helpful? 0
  • +
  • -

#15 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Need a program to restart from a certain point in the code!

Posted 03 January 2008 - 12:06 PM

Thanks for the link, Bench. And also for bringing up the looping rather than functions/methods - I wasn't really thinking my arguments through very clearly, and was a little off-base with some of my arguments about the goto statement.

And Delta_Echo - when people on here say that "X is evil", you have to take it with a grain of salt. In most cases, this means that X is something to be avoided if possible, but NOT if it turnss out to be the best solution for the job. This site has a pretty fair description of what many people seem to mean by it: http://www.parashift...e.html#faq-6.15.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1