Time Issue

Hi,

I wanna create program that will turn my pc off in selected time, I've created this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;

void main()
{
	bool run = true;
	time_t rawtime;
	struct tm * timeinfo;
	char cas[80];
	time(&rawtime);
	timeinfo = localtime (&rawtime);
	strftime(cas,80,"%H:%M", timeinfo);
	while(run)
	{
		if(cas == "22:45")
			run = false;
	}
	system("pause");
}


but it's 22:44, and when it turns on 22:45 nothing happens, I've tried it with strings and I got nothing again, but when I try it with if(cas=cas) it works :-/ any suggestions?

Edit: this program should turn my PC off in time which I'll enter on startup, so don't ask me why I'm doing this, and yes I know there is command in system for shutdown...
Last edited on
Try if(strcmp(cas, "22:45") == 0) instead of if(cas == "22:45").

Useful link -> http://cplusplus.com/reference/clibrary/cstring/strcmp/

EDIT: A couple of other things...

(1) You should update cas inside the loop.
(2) Written like this, your program will waste CPU time.

It should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <windows.h>

//...

while (true)
{
    // update cas

    if(strcmp(cas, "22:45") == 0) break;

    Sleep(10000);
}

//... 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 /*
 * Line 11
 * is a pointer.
 */
char cs[80];

/*
 * Line 17
 * checks to see if the pointers are the same,
 * i.e. if they are stored in the same place in memory.
 * This can only happen if they are the same pointer, or references to the same pointer.
 */
if(cas == "22:45")

/*
 * I believe there is a string equality test in the standard library;
 * or, you could just cast to (or construct) a c++ string.
 */
if( String(cas) == "22:45" )
Last edited on
I've tried both, nothing is working :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <string>
#include <time.h>
using namespace std;

void main()
{
	time_t rawtime;
	struct tm * timeinfo;
	char cas[80];
	time(&rawtime);
	timeinfo = localtime (&rawtime);
	strftime(cas,80,"%H:%M", timeinfo);
	while(true)
	{
		if(strcmp(cas, "9:30") == 0)
			break;
		Sleep(2000);
	}
	system("pause");
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <string>
#include <time.h>
using namespace std;

void main()
{
	time_t rawtime;
	struct tm * timeinfo;
	char cas[80];
	time(&rawtime);
	timeinfo = localtime (&rawtime);
	strftime(cas,80,"%H:%M", timeinfo);
	while(true)
	{
		if(string(cas) == "9:32")
			break;
		Sleep(2000);
	}
	system("pause");
}
Well, I told you that you have to update the string inside the loop :P

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <windows.h>
#include <iostream>
#include <string>
#include <ctime>
using namespace std;

int main()
{
    time_t rawtime;
    struct tm * timeinfo;
    char cas[80];
    
    while(true)
    {
        time(&rawtime);
        timeinfo = localtime (&rawtime);
        strftime(cas,80,"%H:%M", timeinfo);
        
        if(string(cas) == "12:04") break;
        
        Sleep(2000);
    }
    system("pause");
}

The above works fine for me.
Last edited on
ah, thanks, gonna try it!

edit: it works! :) thanks :P
Last edited on
Topic archived. No new replies allowed.