Thread: How to restart a program using loop?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    14

    How to restart a program using loop?

    Im just starting off in C and I wrote this program and I want it to run, then at the end ask if the user wishes to run the program again, how would I do this?

  2. #2
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Do you know what a do-while loop is?

    Here is the tut from this website, if you find it hard to understand for whatever reason, I'll try to explain to you what it is to the best of my limited ability. CLICK
    Last edited by Alan Gott; 12-04-2011 at 04:00 PM.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    Ok this is what I'm trying to use for the loop

    Code:
    do {
    
    then all my code... functions etc.. then at the bottom
    
    do {
       printf(">>>>>Do you want to have another house painted (y-yes / n-no)? ");
                    scanf(" %c", answer);
                if (answer == 'n'){
                    printf("Have a nice day!\n");
                }
    }while (answer != 'y' && answer != 'n');
    
    }while (answer == ' y');
    
    return 0;
    
    }
    I got the loop to go again if the user wishes to paint another house... But all my variables don't reset they just keep readding the numbers from previous houses, how do I get all the variables to reset? I tried putting the do { before the declaring of variables but that doesn't seem to work
    Last edited by NewbieCProgramm; 12-05-2011 at 10:29 AM.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Hard to say since you didn't post the declaration of "answer", but maybe you want &answer in the scanf().

    Also, you should check the return value of scanf to make sure it did what you wanted it to do.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    Oh crud sorry, I posted this asking a different question but figured it out previously and forgot to change it but hes the code im using

    and I set answer to ( answer = 'y'; )
    Code:
    do {
     
    then all my code... functions etc.. then at the bottom
     
    do {
       printf(">>>>>Do you want to have another house painted (y-yes / n-no)? ");
                    scanf(" %c", &answer);
                if (answer == 'n'){
                    printf("Have a nice day!\n");
                }
    }while (answer != 'y' && answer != 'n');
     
    }while (answer == ' y');
     
    return 0;
     
    }
    If i choose 'n' for the answer, it will post have a nice day and end the program, if I hit 'y' it will re-run the program but all the variables will be the same from the previous house and just add them up, I gotta figure out how to reset all the variables so it's pretty much running from scratch again
    Last edited by NewbieCProgramm; 12-05-2011 at 10:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. return to main or restart program
    By behzad_shabani in forum C Programming
    Replies: 5
    Last Post: 06-29-2008, 04:58 AM
  2. Code to restart/reload program
    By GanonsSpirit in forum C++ Programming
    Replies: 7
    Last Post: 09-20-2007, 12:34 AM
  3. restart the program
    By kellymart87 in forum C++ Programming
    Replies: 15
    Last Post: 04-17-2007, 10:04 PM
  4. restart program
    By kellymart87 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2007, 11:18 PM
  5. How to restart a program without closing it...
    By D4050 in forum C++ Programming
    Replies: 16
    Last Post: 10-31-2001, 12:38 PM