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

Similar Threads
thread Thread Starter Forum Replies Last Post
Running a java program on OS X 10.6.8 Stud1ent Mac Programming 2 Nov 10, 2013 08:27 PM
Problem running a program because of Java GrantMeThePower OS X 10.8 Mountain Lion 5 Jan 16, 2013 07:24 PM
Running CAD Program through Sheepshaver? TSE PowerPC Macs 2 Nov 30, 2012 04:02 PM
Best program to buy for running windows on a Macbook LeeRoylee Windows, Linux & Others on the Mac 3 Oct 1, 2012 11:01 PM
Does Reminders still work even though the program isnt running? Ryukouki Mac Applications and Mac App Store 3 Sep 2, 2012 07:52 PM

Forum Jump

All times are GMT -5. The time now is 11:46 AM.

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

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