limited no'of times execution

Discussion in 'C Programming' started by kk, Jul 26, 2005.

  1. kk

    kk Guest

    Hi all,
    How to write a c/c++ program to execute limited number of times.
    if anybody know plz give reply.
    thanks
    kk, Jul 26, 2005
    #1
    1. Advertising

  2. KK,

    Probably you could have the number of times a program executed in a
    persistent storage. Then each time you start the execution, compare
    that with the maximum times you would want the program to execute. If
    less, then increment the value -> write back to the storage,
    automically and proceed normally. Else, abort.

    Hope that helps.

    Regards,
    Sriram.
    Sriram Rajagopalan, Jul 26, 2005
    #2
    1. Advertising

  3. kk

    MCheu Guest

    On 26 Jul 2005 02:05:59 -0700, "kk" <> wrote:

    >Hi all,
    >How to write a c/c++ program to execute limited number of times.
    >if anybody know plz give reply.
    >thanks


    If you mean to loop a specific chunk of code within the program X
    number of times, you can use a FOR loop or a WHILE loop

    FOR loop:
    ===================

    int n; // my counter variable.
    ..
    ..
    ..
    // Executes the code inside the loop 4 times.

    for (n=0; n < 4; n++)
    {
    [your code goes here]
    }


    WHILE LOOP:
    -loops so long as the condition in the brackets (n > 0) is true.
    ======================
    int n=4;

    while (n > 0)
    {
    [your code goes here]
    n--;
    };

    Do WHILE LOOP
    -A variation of the While loop:
    ==================================
    int n=4;
    do
    {
    [your code goes here]
    n--;
    }
    while (n > 0);


    With a DO-WHILE, the condition test isn't done until it's gone through
    the code, so no matter what happens, the stuff inside the DO-WHILE
    loop will run at least once. Even if the condition is totally false
    to begin with. With the WHILE loop, if the condition is false, the
    test happens at the beginning and if the condition is false, it will
    skip over the code in the loop and it will never be run.


    Try it yourself by setting n=0 using the examples shown above.


    ---------------------------------------------------------------------------

    If you mean to limit the number of times that the user may run your
    program, that's more of a program design thing than a language thing.
    I'd imagine you'd have to use file IO or some feature of the OS to
    store a counter value. Load up the old value each time it's run, and
    increment the file value at some point during the program's run (at
    start, after the initial check, or just before exit). It'd be pretty
    easy to defeat, unless you disguised the way it was stored in some
    way. All of this stuff is somewhat off topic and a bit more than I'd
    like to think about at this early hour though.
    ---------------------------------------------

    MCheu
    MCheu, Jul 26, 2005
    #3
  4. kk

    SSM Guest

    Hello,

    You can put command line argument and inside your program, you can repeat
    the functionality required by a loop which has number of iterations=command
    line argument.

    Regards,
    -SSM

    "kk" <> wrote in message
    news:...
    > Hi all,
    > How to write a c/c++ program to execute limited number of times.
    > if anybody know plz give reply.
    > thanks
    >
    SSM, Jul 26, 2005
    #4
  5. kk

    kk Guest

    Hello,

    what my mean is, i execute the program from command line. Assume it you
    can execute your program only 10 no'of times. Execution at 11th time it
    should not allow to execute program.
    it may possible throught volatile int but not sure. if anybody have an
    idea or have a program, plz send it.
    thanks in advance.

    Regards,
    kk
    kk, Jul 26, 2005
    #5
  6. kk

    SSM Guest

    Hello KK,

    Then what I can think of is:

    Create one file if it is not existing, store integer count zero in it.

    Next time whn you run, since the file already exist, you have to read count
    (2nd time it will read zero),
    compare against MAX_COUNT if less then increment and store in the file
    again.

    At some point the counter will reach maximum value and then program should
    not execute.

    Regards,
    SSM


    "kk" <> wrote in message
    news:...
    > Hello,
    >
    > what my mean is, i execute the program from command line. Assume it you
    > can execute your program only 10 no'of times. Execution at 11th time it
    > should not allow to execute program.
    > it may possible throught volatile int but not sure. if anybody have an
    > idea or have a program, plz send it.
    > thanks in advance.
    >
    > Regards,
    > kk
    >
    SSM, Jul 26, 2005
    #6
  7. In article <>,
    kk <> wrote:
    >
    >
    > Hi all,
    > How to write a c/c++ program to execute limited number of times.
    > if anybody know plz give reply.
    > thanks
    >


    chellapas is that u?

    Anyway, one amusing way to implement limited execution is
    to open the running program itself and decrement a "constant"
    in the program. At run time, check that the constant hasn't
    been decremented too much. Some OS's don't let you do this
    sort of thing, but many do.

    In fact, I suspect there is nothing in the standard that
    would prevent one from making a strictly conforming
    program behave in a non-conforming manner by deleting or
    overwriting itself!
    --
    7842++
    Anonymous 7843, Jul 26, 2005
    #7
  8. "kk" <> writes:
    > what my mean is, i execute the program from command line. Assume it you
    > can execute your program only 10 no'of times. Execution at 11th time it
    > should not allow to execute program.
    > it may possible throught volatile int but not sure. if anybody have an
    > idea or have a program, plz send it.
    > thanks in advance.


    A volatile int is unlikely to be helpful here.

    Why do you want to do this?

    The most obvious way is to use a file to record how many times the
    program has executed. That's probably good enough if you're not
    worried about the user manually modifying or removing the file.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jul 26, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bWF2cmlja18xMDE=?=

    SetAuthCookie works some times and fails some times?

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Mar 23, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    512
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Mar 23, 2006
  2. =?Utf-8?B?bWF2cmlja18xMDE=?=

    Forms Authentication Fails some times and not some times???

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Mar 28, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    488
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Mar 28, 2006
  3. djskrill
    Replies:
    9
    Views:
    698
    djskrill
    Oct 1, 2003
  4. petersson
    Replies:
    2
    Views:
    124
    J├╝rgen Exner
    Jul 22, 2004
  5. petersson
    Replies:
    2
    Views:
    99
    Jonathan Tree
    Jul 22, 2004
Loading...

Share This Page