Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

pongster08

macrumors newbie
Original poster
May 26, 2009
8
0
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!
 
The other option is you could use a switch statement.

Code:
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.
 
The other option is you could use a switch statement.

Code:
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
 
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
 
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
 
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
Code:
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"
Code:
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"
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.