PDA

View Full Version : C++ program re-running it!




pongster08
May 27, 2009, 08:48 PM
I have made a program that asks a series of questions and requires you to answer them.. once you answer them, it takes your input goes thru a few if statements and prints your answer.. at the end i want to make a option to run the program again.. it should look like
if (answer == 'y' || answer == 'Y')
{

...
...
...

}
if the answer is y or Y how can i tell it to recall the program.

Thank you!



pongster08
May 27, 2009, 08:58 PM
Solved using a while loop

AUFan
May 28, 2009, 12:03 PM
The other option is you could use a switch statement.


switch(answer)
{
case 'Y':
case 'y':
actions. . .
break;
}


So on and so forth. It would let you test for multiple answers without having to do lots of if testing.

lee1210
May 28, 2009, 01:30 PM
The other option is you could use a switch statement.


switch(answer)
{
case 'Y':
case 'y':
actions. . .
break;
}


So on and so forth. It would let you test for multiple answers without having to do lots of if testing.

You'd still need this in a while or some other sort of loop structure, though...

-Lee

SRossi
May 28, 2009, 02:27 PM
I would have made a new function and added the question into that then just added an if statement into it to see if it was 'y' or 'Y' then restarted the main function or whatever function the program starts in, if it returned no I would have gave an error and returned to the last function therefore ending the application.

Thats the way I would have done it but a while loop works easier :)

Stephen

lee1210
May 28, 2009, 02:47 PM
I would have made a new function and added the question into that then just added an if statement into it to see if it was 'y' or 'Y' then restarted the main function or whatever function the program starts in, if it returned no I would have gave an error and returned to the last function therefore ending the application.

Thats the way I would have done it but a while loop works easier :)

Stephen

Re-calling main in this manner can lead to unbounded stack growth. In most cases this isn't a huge deal, but it's worth taking notice of this.

-Lee

autorelease
May 28, 2009, 03:00 PM
To reiterate what Lee said:

NEVER CALL main(). THERE IS NO EXCUSE.

(Unless you're writing an IOCCC entry or something.)

Sander
May 28, 2009, 03:23 PM
(Unless you're writing an IOCCC entry or something.)

Or an OS.

autorelease
May 28, 2009, 04:05 PM
Or an OS.

Technically, main() is called by the C runtime (usually called crt0), which is part of the standard library. Same difference.

SRossi
May 28, 2009, 06:19 PM
What I was meaning about main was the main function that the program ran from, not main().

Sorry about the misunderstanding

Stephen

EMT
Jun 1, 2009, 04:02 AM
I would have made a new function and added the question into that then just added an if statement into it to see if it was 'y' or 'Y' then restarted the main function or whatever function the program starts in, if it returned no I would have gave an error and returned to the last function therefore ending the application.

Thats the way I would have done it but a while loop works easier

Stephen


write a function that returns bool
bool MyFunction()
{
printf();//Your prompt to user
scanf();// User's reply
if (expression)// if user's reply is 'Y' or 'y'
{ // return true
return 1;
}
else
{ // if not return false
return 0;
}
}


Use do while to repeat your "main function"
do
{
bool myBool;
//your code
myBool=MyFunction();//call your function , get the return value of your function

}while(myBool) if true, repeat your "main function"