limited no'of times execution

K

kk

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

Sriram Rajagopalan

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.
 
M

MCheu

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.
 
S

SSM

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
 
K

kk

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
 
S

SSM

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
 
A

Anonymous 7843

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!
 
K

Keith Thompson

kk said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,167
Messages
2,570,911
Members
47,453
Latest member
MadelinePh

Latest Threads

Top