C ++ code任务调度 [英] C++ code for task scheduling
问题描述
这code没有错误,但后来当我执行它,没有输出,程序自动关闭话说程序已停止工作。
#包括LT&;&unistd.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&time.h中GT;
#包括LT&;&string.h中GT;
诠释的main()
{
字符* timetoken;
炭currtime [7];
炭schedtime [7];
INT I;
结构TM * localtimeptr;
的strcpy(schedtime15:25:00);
而(6!= 9)
{
time_t的LT;
睡眠(1);
LT =时间(NULL);
localtimeptr =本地时间(LT);
timetoken =的strtok(asctime(localtimeptr),);
为(ⅰ= 1; I&小于5;我+ +)
timetoken = strtok的('\\ 0',);
如果(我== 3)
{
的strcpy(currtime,timetoken);
}
}
的printf(现在时间是:%S \\ n,currtime);
的printf(我们正在等待:%S \\ n,schedtime);
如果(!STRCMP(currtime,schedtime))
{
的printf(时间做的东西\\ n);
系统(C:\\ PROJECT X);
}
残培();
返回0;
}
我不知道你想做什么,但这是腥:
而(6!= 9)
{
/ * ... * /
}
/ * ...更多code ... * /
6永远不等于9,所以这是一个无限循环。有没有办法打出来的循环,所以没什么越code部分将被执行。这意味着你的的printf
旨意不执行,也不会对你的系统
电话。你需要一些摆脱这种循环。
为了使code更容易阅读(应该始终是头等大事!的),我会建议只是写
,而(真){
...
}
要更清楚地表明循环应该运行之前,您明确破
从它。
另注:此code
系统(C:\\ PROJECT X);
是不正确的,因为C ++会间preT \\ P
作为转义字符。为了解决这个问题,逃不过你的斜线:
系统(C:\\\\项目X);
有关另一个bug,在这个循环仔细一看:
为(i = 1; I< 5;我++)
timetoken = strtok的('\\ 0',);
如果(我== 3)
{
的strcpy(currtime,timetoken);
}
C ++是跨preting这是
为(i = 1; I< 5;我++){
timetoken = strtok的('\\ 0',);
} 如果(我== 3)
{
的strcpy(currtime,timetoken);
}
从这里,它更清晰,这是行不通的,因为如果
语句是外循环。因此, I
从不3.您大概意思
为(i = 1; I< 5;我++){
timetoken = strtok的('\\ 0',);
如果(我== 3)
{
的strcpy(currtime,timetoken);
}
}
使清理你的code格式(缩进,空格等)将帮助prevent这种错误的努力。如果你已经宣布 I
本地for循环,那么你很可能会发现这点。例如,该code不能编译:
的for(int i = 1; I< 5;我++){
timetoken = strtok的('\\ 0',);
} 如果(ⅰ== 3)// i不是在范围
{
的strcpy(currtime,timetoken);
}
作为一般规则,推迟声明变量,直到你绝对需要他们。它使code更容易阅读(使用中的变量往往是接近code。使用它们)和bug更少(如上图所示)。
希望这可以帮助您开始!
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('\0'," ");
if(i==3)
{
strcpy(currtime,timetoken);
}
}
printf("The current time is: %s\n",currtime);
printf("We are waiting for: %s\n",schedtime);
if(!strcmp(currtime,schedtime))
{
printf("Time to do stuff \n");
system("C:\PROJECT X");
}
getch();
return 0;
}
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 printf
s 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('\0'," ");
if(i==3)
{
strcpy(currtime,timetoken);
}
C++ is interpreting this as
for(i=1;i<5;i++) {
timetoken=strtok('\0'," ");
}
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('\0'," ");
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('\0'," ");
}
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!
这篇关于C ++ code任务调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!