Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 27, 2009, 08:48 PM   #1
pongster08
macrumors newbie
 
Join Date: May 2009
C++ program re-running it!

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!

Last edited by pongster08; May 27, 2009 at 08:53 PM. Reason: .
pongster08 is offline   0 Reply With Quote
Old May 27, 2009, 08:58 PM   #2
pongster08
Thread Starter
macrumors newbie
 
Join Date: May 2009
NVM Solved!

Solved using a while loop
pongster08 is offline   0 Reply With Quote
Old May 28, 2009, 12:03 PM   #3
AUFan
macrumors newbie
 
Join Date: Feb 2008
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.
AUFan is offline   0 Reply With Quote
Old May 28, 2009, 01:30 PM   #4
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by AUFan View Post
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
lee1210 is offline   0 Reply With Quote
Old May 28, 2009, 02:27 PM   #5
SRossi
macrumors regular
 
Join Date: May 2009
Location: Glasgow, Scotland
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
__________________
macMini, 2.0 GHz Core 2 Duo, 1 GB RAM, 120GB hd;
Dell S2209W 21.5" monitor;
iPhone 4GS 16GB;
30 GB iPod video;
SRossi is offline   0 Reply With Quote
Old May 28, 2009, 02:47 PM   #6
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by SRossi View Post
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
lee1210 is offline   0 Reply With Quote
Old May 28, 2009, 03:00 PM   #7
autorelease
macrumors regular
 
Join Date: Oct 2008
Location: Achewood, CA
To reiterate what Lee said:

NEVER CALL main(). THERE IS NO EXCUSE.

(Unless you're writing an IOCCC entry or something.)
__________________
*** -[NSAutoreleasePool autorelease]: Cannot autorelease an autorelease pool
autorelease is offline   0 Reply With Quote
Old May 28, 2009, 03:23 PM   #8
Sander
macrumors 6502
 
Join Date: Apr 2008
Quote:
Originally Posted by autorelease View Post
(Unless you're writing an IOCCC entry or something.)
Or an OS.
Sander is offline   0 Reply With Quote
Old May 28, 2009, 04:05 PM   #9
autorelease
macrumors regular
 
Join Date: Oct 2008
Location: Achewood, CA
Quote:
Originally Posted by Sander View Post
Or an OS.
Technically, main() is called by the C runtime (usually called crt0), which is part of the standard library. Same difference.
__________________
*** -[NSAutoreleasePool autorelease]: Cannot autorelease an autorelease pool
autorelease is offline   0 Reply With Quote
Old May 28, 2009, 06:19 PM   #10
SRossi
macrumors regular
 
Join Date: May 2009
Location: Glasgow, Scotland
What I was meaning about main was the main function that the program ran from, not main().

Sorry about the misunderstanding

Stephen
__________________
macMini, 2.0 GHz Core 2 Duo, 1 GB RAM, 120GB hd;
Dell S2209W 21.5" monitor;
iPhone 4GS 16GB;
30 GB iPod video;
SRossi is offline   0 Reply With Quote
Old Jun 1, 2009, 04:02 AM   #11
EMT
macrumors newbie
 
Join Date: May 2009
Quote:
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"

Last edited by EMT; Jun 1, 2009 at 07:10 PM.
EMT is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
uninstalling programs (is it realy that easy)? ethan101 OS X 23 Feb 25, 2007 05:24 AM
Requesting a program if it exist..... x.x kitki83 Mac Applications and Mac App Store 3 Oct 16, 2006 04:54 PM
Can I run a program without it being on the dock? GreyFox1221 Mac Basics and Help 2 Jan 11, 2006 11:20 PM
iBook G3 Logic Board Repair Program, is it still alive? ReanimationLP PowerPC Macs 5 Jan 3, 2006 11:33 AM
Front Row. An Awsome Program..if it worked properly 59031 Mac Applications and Mac App Store 0 Nov 12, 2005 01:06 AM


All times are GMT -5. The time now is 04:21 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC