Jump to content

C Restarting a program

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
cook777

cook777

    Newbie

  • Members
  • PipPip
  • 13 posts
Is there a way to restart a program from the biggining.

What I want is something like:

printf ("data is wrong do you want to re-enter it or do onther student.\n");

I like to give the person a chose to reenter it or just start over the program.

I know there a way to end just wander if they have one for this. If not I have to do some breack loop back in the int main() and I do not really want to do that.

Thanks

#2
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
I'm tempted to say use a goto statement. But thats not very popular or favored amongst programmers. It would be nice to see your code.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#3
julmuri

julmuri

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts

fread said:

I'm tempted to say use a goto statement.
Rather then goto i would use some loop statement with continue & break, ie:


	for ( ;; )

	{

		int result;


		result = 0;

		result = getInput();

		if ( 0 == result )

		{

			// fail;

			continue;

		} // if


		break;

	} // for



#4
cook777

cook777

    Newbie

  • Members
  • PipPip
  • 13 posts
Ok I not going to put the real program in here because its too long. So I just wroght this one that should have the idea. There maybe mitakes I have not run in though compiler or debuged it.


#include <stdio.h>

#include <ctype.h>


void getClass (int class);

void getHours (int hours);


int main ()

{

     int class[1]; int hours[1]; int x=1; char option;


     do{

          getClass (class);

          getHours (hours);

          

          printf ("Do you want to do anther student? Y for yes and N for No n");

          scanf ("%c",option); toupper(option)

     

          if (option == 'N')  

               x=0;

     

     }while (x==1);


     return 0;

}


void getClass (int class)

{

     printf ("Enter in the class number");

     scanf ("%d",class[0]);

     printf ("\n");

}


getHours (int hours)

{

     case 1234 : hours[0] = 3;

     case 2134 : hours[0] = 1;

     case 1243 : hours[0] = 3;

     case 1324 : hours[0] = 4;

     default : // This is were I like it to start the program over.

}


Ya this is much more simple code but the idea is there. Hope you guys understand it better now.

Edited by cook777, 24 March 2009 - 11:00 AM.
fix code