C++ program re-running it!

Discussion in 'Mac Programming' started by pongster08, May 27, 2009.

  1. macrumors newbie

    Joined:
    May 26, 2009
    #1
    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!
     
  2. thread starter macrumors newbie

    Joined:
    May 26, 2009
    #2
    NVM Solved!

    Solved using a while loop
     
  3. macrumors newbie

    Joined:
    Feb 2, 2008
    #3
    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.
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    You'd still need this in a while or some other sort of loop structure, though...

    -Lee
     
  5. macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #5
    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
     
  6. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    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
     
  7. macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
    #7
    To reiterate what Lee said:

    NEVER CALL main(). THERE IS NO EXCUSE.

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

    Joined:
    Apr 24, 2008
    #8
    Or an OS.
     
  9. macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
    #9
    Technically, main() is called by the C runtime (usually called crt0), which is part of the standard library. Same difference.
     
  10. macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #10
    What I was meaning about main was the main function that the program ran from, not main().

    Sorry about the misunderstanding

    Stephen
     
  11. EMT
    macrumors newbie

    Joined:
    May 25, 2009
    #11

    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"
    
     

Share This Page