Hello,

i am a beginner and need your help. The little programm should run and close another aplication(this works without the loop) every x seconds but it doesnt work :( .

Thank you for your help.

#include <stdlib.h>
#include <time.h>
#include<iostream>
void start (int);
using namespace std;



int main ()
{
    int arrayLength= 0 ;       //array length
    int Time;
    int time[arrayLength];    //array with all times
    time[0]=20;  
    
  int j;
 
  clock();
   
 
  for (;;)
   {
          
          for(j=0;j<=arrayLength;j++)
          { 
                Time=time[j];
                int w = Time;// time modifier
                //cout<< clock()/CLOCKS_PER_SEC<<"\n";
                
                if((clock()/CLOCKS_PER_SEC)==Time)
            
                    {
                                                
                       time[j]+=w;                
                       start (j);
                     
                     }
          }
  }
 
 return 0; 
}

void start (int x)
{
     if(x=0)
     {
     system("C:\\Control\\c++\\Test.exe");
     system("C:\\Control\\c++\\do1.exe");
     system("C:\\Control\\c++\\do2.exe");
     system("C:\\Control\\c++\\Testend.exe");
     }
 }

>>The little programm should run and close another aplication

Nope. Not even close to doing that. It is starting 4 programs, not stopping them.

>>int time[arrayLength];
Rename that array because time is a function name.

>> int w = Time;
why? you don't need that extra variable in your program.

>>if((clock()/CLOCKS_PER_SEC)==Time)

why are you using clock() instead of time()? if( time(0) >= Time) You can't check if time() is equal to something because that may or may not ever occur. Check for >= instead of ==

thx for your reply...

The Testend.exe close the others.
I have renamed that array and tried with

f( time(0) >= Time)

, But the result is the same.

The statement

if((clock()/CLOCKS_PER_SEC)>=Time)

or

if( time(0) >= Time)

are allways false.

any other advice?

int arrayLength = 0;
int time[arrayLength];

Is that even legal? Can you have a zero-length array? I know some compilers are starting to allow the use of a non-const variable to determine an array's size, but mine doesn't. When I try to compile your original code, other than the variable-as-size error mentioned I get 2 other errors, both also related to this declaration. They basically say "You're declaring a 0-length array, I don't know how to make that."

Even if your compiler somehow accepts this, you'll most likely be overrunning the boundaries on the first iteration of your for loop which would probably explain your weird comparison results. When you overrun boundaries, you usually start reading unpredictable values.

Edited 6 Years Ago by Fbody: n/a

So a zero-length array is okay? Does it get treated as some sort of dynamic array or something?

>>So a zero-length array is okay?

Maybe yes, and maybe no, depending on the compiler. I don't know exactly how its treated because I don't have a compiler that allows it. But I suspect that its the same as passing 0 to malloc int *array = new int[0]; or int *array = (int *)malloc(0);

i am using Dev c++ on win7 (64bit ) and there are no errors.

But this was the problem. thank you all.

i am using Dev c++ on win7 (64bit ) and there are no errors.

If you are compiling with GCC, then try compiling the code with the following two compiler switches:

-Wall
-pedantic

You'll probably be alerted to at least two problems.

This article has been dead for over six months. Start a new discussion instead.