Thread: Application Repeatition

  1. #1
    Registered User
    Join Date
    Jun 2009

    Application Repeatition

    How does one code an application to repeat? My application functions fine but I need to have the program start from the beginning after it concludes. Their must be a command when I am by (return).
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    //Function Declaration
    int main(int argc, char *argv[])
    //Local Declarations
        int a= 1;         //First Integer
        int b= 0;         //Least Allowed Number
        int x;            //User inputted Number
        int sum;          //Sum of numbers multiplied
        printf("Input a Number:");       //User Instructions
        scanf("%d", &x);                   //User inputs number
      if (x >= b)                          //If statement
        sum = x * a;                       //Number Multiplied by 1 and results
        a = a+1;
        printf("%d*1=%d\n\n", x, sum);
        sum = x * a;                       //Number Multiplied by 2 and results
        a = a+1;
        printf("%d*2=%d\n\n", x, sum);
        sum = x * a;                       //Number Multiplied by 3 and results
        a = a+1;
        printf("%d*3=%d\n\n", x, sum);
        sum = x * a;                       //Number Multiplied by 4 and results
        a = a+1;
        printf("%d*4=%d\n\n", x, sum);
        sum = x * a;                       //Number Multiplied by 5 and results
        a = a+1;
        printf("%d*5=%d\n\n", x, sum);
        sum = x * a;                       //Number Multiplied by 6 and results
        a = a+1;
        printf("%d*6=%d\n\n", x, sum);
        sum = x * a;                       //Number Multiplied by 7 and results
        a = a+1;
        printf("%d*7=%d\n\n", x, sum);
        sum = x * a;                       //Number Multiplied by 8 and results
        a = a+1;
        printf("%d*8=%d\n\n", x, sum);
        sum = x * a;                       //Number Multiplied by 9 and results
        a = a+1;
        printf("%d*9=%d\n\n", x, sum);
        sum = x * a;                       //Number Multiplied by 10 and results
        a = a+1;
        printf("%d*10=%d\n\n", x, sum);
      else                                 //Closes program with Negative Integer
         return 0;
      return 0;
    Last edited by Salem; 06-16-2009 at 02:29 PM. Reason: Added [code][/code] tags, learn to use them

  2. #2
    Registered User
    Join Date
    Jun 2009
    if you post your code you will get better help.

    You could always just enclose your whole program in a while loop and check the condition after each iteration of your program.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Usually, that is done using loops of some sort. C supports three loop constructs:
    • for
      Normally for when you have a fixed [1] number of repetitions to do. Not necessarily fixed at the time of writint the code, but there is a distinct "we start at here, and go until we get to this point" type thing.
    • while
      For when you have a situation where repetition at all may happen
    • do .. while
      When you have a situation that always at least ONE repetition should happen.

    There is two other ways to cause repetition in C:
    • Recursion.
      You call function f() inside function f(). Note that this is illegal for main in C++, but technicaly legal in C. Generally, this is not what you should do for repetition unless you have a particular type of problem that lends itself to recursive solution. You should not do this if you don't know how many loops it may be - do too many loops, and the program crashes.
    • Goto [and setjmp/longjmp]
      You can make loops with goto, but goto is not recommended unless you have explored all other solutions and come up with worse solutions in all cases. I use maybe one goto every 4-5 months in my code - generally in cases where I'm in deep nested loops and need to get out quick because things are horribly wrong (e.g. can't allocate memory and need to clean something up before returning with an error code). Do not use this method for simply repeating a piece of code again.

    Last edited by matsp; 06-16-2009 at 02:08 PM. Reason: Fix list tag
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Perhaps you want a while loop which will repeat some code until the user requests a quit?

    For example,
    char item;
    do {
        item = getchar();
        while(getchar() != '\n') {}  /* remove characters until newline */
    } while(item != 'q');
    [edit] Don't forget that [/list], matsp. [/edit]

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cleanup of the application...
    By Petike in forum Windows Programming
    Replies: 1
    Last Post: 08-16-2008, 05:23 PM
  2. Problem with com application
    By amardon in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2005, 05:50 AM
  3. MFC run application by clicking on file...
    By dug in forum Windows Programming
    Replies: 4
    Last Post: 12-02-2004, 04:33 AM
  4. Win application not very portable
    By swed in forum Windows Programming
    Replies: 5
    Last Post: 10-01-2001, 11:17 AM