Program freezes up every time I run it

Hello!
I made a c++ program that is supposed to print the date and time every second, and clearing the screen every 10 seconds. I used this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
using namespace std;
void wait ( int mseconds )
{
  clock_t endwait;
  endwait = clock () + mseconds;
  while (clock() < endwait) {}
}
int main(){
  int i = 1;
  while(1){
    if (i > 10) system("cls");
    time_t *now;
    struct tm *time_info;
    time(now);
    time_info = localtime(now);
    printf("Current date/time: %s", asctime(time_info));
    wait(1000);
    i++;
  }
  return 0;
}
  

I compiled it with th MinGW C++ Compiler successfully, and ran it on Windows Command Prompt. However, it keep freezing up every time I run it, without even printing anything. I commented out all the stdlib.h lines:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <time.h>
//#include <stdlib.h>
using namespace std;
void wait ( int mseconds )
{
  clock_t endwait;
  endwait = clock () + mseconds;
  while (clock() < endwait) {}
}
int main(){
  //int i = 1;
  while(1){
    //if (i > 10) system("cls");
    time_t *now;
    struct tm *time_info;
    time(now);
    time_info = localtime(now);
    printf("Current date/time: %s", asctime(time_info));
    wait(1000);
    //i++;
  }
  return 0;
}
  

Now it works, but without clearing the screen. Why is the program freezing when I use stdlib.h, and how can I do this without freezing up the program?
Thanks!
This:
1
2
3
    time_t *now;
    struct tm *time_info;
    time(now);
should be:
1
2
3
    time_t now;
    struct tm *time_info;
    time(&now);
Worked fine on my mac. Could be something with your compiler.
@kbw: thanks for that tip. I thought that if you declare time_t now as a pointer, you would not need to pass now by reference (just like struct tm *time_info). I changed my code to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
using namespace std;
void wait ( int mseconds )
{
  clock_t endwait;
  endwait = clock () + mseconds;
  while (clock() < endwait) {}
}
int main(){
  while(1){
    system("cls");
    time_t now;
    struct tm *time_info;
    time(&now);
    time_info = localtime(&now);
    printf("Current date/time: %s", asctime(time_info));
    wait(1000);
  }
  return 0;
}

This turns the Command Prompt into a clock! But, why does delaring time_t now as a pointer cause the code to freeze up?
Last edited on
Topic archived. No new replies allowed.