Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

C++ code for task scheduling

This code has no errors but then when i execute it, there is no output and the program automatically shuts down saying the program has stopped working.

#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
int main()
{
char *timetoken;
char currtime[7];
char schedtime[7];
int i;
struct tm *localtimeptr;
strcpy(schedtime,"15:25:00");
while(6!=9)
{
   time_t lt;
   sleep(1);
   lt = time(NULL);
   localtimeptr = localtime(lt);
   timetoken=strtok(asctime(localtimeptr)," ");
   for(i=1;i<5;i++)
   timetoken=strtok(''," ");
   if(i==3)
   {
           strcpy(currtime,timetoken);
    }
}
           printf("The current time is: %s
",currtime);
           printf("We are waiting for: %s
",schedtime);
           if(!strcmp(currtime,schedtime))
           {
                                          printf("Time to do stuff 
");
                                          system("C:PROJECT X");
            }        
            getch();
            return 0;                      
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I am not sure what you're trying to do, but this is fishy:

while(6!=9)
{
   /* ... */
}
/* ... more code ... */

6 will always not equal 9, so this is an infinite loop. There's no way to break out of the loop, and so nothing in the "more code" section will execute. This means that your printfs won't execute, nor will your system call. You need some way out of this loop.

To make the code easier to read (which should always be a top priority!), I would suggest just writing

while (true) {
    ...
}

to make it clearer that the loop is supposed to run until you explicitly break from it.

Another note: this code

system("C:PROJECT X");

Is incorrect, because C++ will interpret P as an escape character. To fix this, escape your slash:

system("C:\PROJECT X");

For another bug, look closely at this loop:

   for(i=1;i<5;i++)
   timetoken=strtok(''," ");
   if(i==3)
   {
       strcpy(currtime,timetoken);
   }

C++ is interpreting this as

   for(i=1;i<5;i++) {
       timetoken=strtok(''," ");
   }

   if(i==3)
   {
       strcpy(currtime,timetoken);
   }

From here it's clearer that this won't work, since the if statement is outside the loop. Consequently, i is never 3. You probably meant

   for(i=1;i<5;i++) {
       timetoken=strtok(''," ");
       if(i==3)
       {
           strcpy(currtime,timetoken);
       }
   }

Making the effort to clean up your code formatting (indentation, whitespace, etc.) will help prevent this sort of error. If you had declared i as local to the for loop, then you probably would have spotted this earlier. For example, this code doesn't compile:

   for(int i = 1; i < 5; i++) {
       timetoken=strtok(''," ");
   }

   if(i==3) // i is not in scope
   {
       strcpy(currtime,timetoken);
   }

As a general rule, defer declaring variables until you absolutely need them. It makes the code easier to read (variables in use tend to be close to the code using them) and less buggy (as shown above).

Hope this helps you get started!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...