Thread: help with time.h and threads

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    5

    help with time.h and threads

    I created a program to display time continuesly so it auto updates every sec but the issue is i cant integrate the code in the main program because to make time update continuesly it needs a loop a infinite loop so none of the rest program gets to work. My q is : is it possible to display time continuesly and make the rest of program continue normally? like a multi thread program because i want to display time every sec in the bottom of program. By the way this is console app not gui

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The easy way is to make your input non-blocking.
    Code:
    do {
      if ( keyIsPressed() ) {
        readKey();
        processKey();
      }
      updateTime();
    } while ( 1 );
    If you're using functions like scanf or fgets, then all you need to do is make non-blocking versions of these, so that you can update time whenever you like.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    Non blocking? So for example all my code goes below thw do whileloop? Also i have a technical difficulty i am suppose to display two random num but every time i restart the program it starts displaying the same pattern not the same number but same pattern of num why?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps it would be better if you posted some code, then we could give you direct answers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    insert
    Code:
    #include <conio2.h>
    #include <windows.h>
    
    #include <time.h>
    #include <clock.h>
    struct text_info info;
      struct char_info * screen_buffer;
      int i;
      int cursor_pos_x, cursor_pos_y;
    void sorteo();
    int message();
    int choice;
    char Message[]="RANDOM NUMS!";
    int main()
    {
    
      char password[10];
    
    
    
    
      /* we want to restore current text attributes on the end of application */
      inittextinfo();
    
      /* get the info about screen */
      gettextinfo( &info );
    
      /* get the info about screen */
      gettextinfo( &info );
    
      /* paint border around screen */
      gotoxy( 1, 1 );
      for (i = 1; i <= info.screenwidth; i++) putch( '*' );
      gotoxy( 1, info.screenheight );
      for (i = 1; i < info.screenwidth; i++) putch( '*' );
      for (i = 2; i < info.screenheight; i++) {
          putchxy( 1, i, '*' );
          putchxy( info.screenwidth, i, '*' );
      }
    
        textbackground(13);
        _setcursortype(_SOLIDCURSOR);
        textcolor(5);
        clrscr();
    
        gotoxy(30,2);
        printf("%s\n\n",Message);
        message();
    
    
    return 0;
    }
    
    void sorteo(void)
    {
    
        printf("\nLos nums son: ");
        for(int i=1;i<3;i++)
    
        {
            for(int q=0;q<=200000000;q++);
            printf("%d  ",rand()%48 + 1);
    
    
        }
        gotoxy(20,15);
        printf("\nPresione cualquier tecla para continuar\n");
           gotoxy(40,15);
            getche();
    
    }
    
    
    
    int message()
        {
              for (int i = 1; i <= info.screenwidth; i++) putch( '*' );
      gotoxy( 1, info.screenheight );
      for (int i = 1; i < info.screenwidth; i++) putch( '*' );
      for (int i = 2; i < info.screenheight; i++) {
          putchxy( 1, i, '*' );
          putchxy( info.screenwidth, i, '*' );
      }
    
    
            gotoxy(30,5);
            printf("Menu del Programa\n");
    
            gotoxy(20,7);
            printf("Entra uno (1) para recibir numeros del sorteo\n");
    
            gotoxy(20,9);
            printf("Entra uno (2) para salir del programa\n");
    
            gotoxy(20,10);
            printf("Su respuesta es : ");
    
            gotoxy(39,10);
            scanf("\n\n%i",&choice);
             gotoxy( 1, 1 );
    
    
    
            switch(choice)
            {
           case 1:
            sorteo();
            break;
    
           case 2:
            system("cls");
            gotoxy(30,4);
            printf("Adios, vuelva pronto!!\n\n\n\n\n\n");
            getch();
            exit(1);
    
            }
            system("cls");
            gotoxy(25,5);
            char otra;
            while(otra!='n')
            {
                system("cls");
                gotoxy(19,5);
                printf("Desea volver a intentar otros numeros? [y/n]\n\n");
                gotoxy(65,5);
                otra = getch();
                if(otra=='n')
                {
                    system("cls");
                    gotoxy(30,4);
                    printf("Adios, vuelva pronto!!\n\n\n\n\n\n");
                    Sleep(200);
                    break;
                }
    
                else;
                sorteo();
    
            }
    
    
    
    
        }
    Salem like i said it always prints same 3random nums sequence when i start the prog

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    Quote Originally Posted by thewhisper View Post
    it always prints same 3random nums sequence when i start the prog
    rand() is pseudo-random number generator. Try srand() for more pseudo-randomness.

    For threading, look at process.h and _beginthread().

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    I am dealing with graphics a lot and i have a problem with sdl and allegro i cant get a nice tut on both allegro 5 or sdl so i prefer simpke stuff like 2D MENU and simple graphics like the menu from borland turbo c that works with c purely which one you recommend me

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your indentation needs work.
    Code:
    #include <conio2.h>
    #include <windows.h>
    #include <time.h>
    #include <clock.h>
    
    struct text_info info;
    struct char_info *screen_buffer;
    
    int i;
    int cursor_pos_x, cursor_pos_y;
    void sorteo();
    int message();
    int choice;
    char Message[] = "RANDOM NUMS!";
    
    int main()
    {
      char password[10];
    
      /* we want to restore current text attributes on the end of application */
      inittextinfo();
    
      /* get the info about screen */
      gettextinfo(&info);
    
      /* get the info about screen */
      gettextinfo(&info);
    
      /* paint border around screen */
      gotoxy(1, 1);
      for (i = 1; i <= info.screenwidth; i++)
        putch('*');
    
      gotoxy(1, info.screenheight);
      for (i = 1; i < info.screenwidth; i++)
        putch('*');
    
      for (i = 2; i < info.screenheight; i++) {
        putchxy(1, i, '*');
        putchxy(info.screenwidth, i, '*');
      }
    
      textbackground(13);
      _setcursortype(_SOLIDCURSOR);
      textcolor(5);
      clrscr();
    
      gotoxy(30, 2);
      printf("%s\n\n", Message);
      message();
    
      return 0;
    }
    
    void sorteo(void)
    {
      printf("\nLos nums son: ");
      for (int i = 1; i < 3; i++)
      {
        for (int q = 0; q <= 200000000; q++);
        printf("%d  ", rand() % 48 + 1);
      }
    
      gotoxy(20, 15);
      printf("\nPresione cualquier tecla para continuar\n");
      gotoxy(40, 15);
      getche();
    }
    
    
    
    int message()
    {
      for (int i = 1; i <= info.screenwidth; i++)
        putch('*');
    
      gotoxy(1, info.screenheight);
      for (int i = 1; i < info.screenwidth; i++)
        putch('*');
      
      for (int i = 2; i < info.screenheight; i++) {
        putchxy(1, i, '*');
        putchxy(info.screenwidth, i, '*');
      }
    
      gotoxy(30, 5);
      printf("Menu del Programa\n");
    
      gotoxy(20, 7);
      printf("Entra uno (1) para recibir numeros del sorteo\n");
    
      gotoxy(20, 9);
      printf("Entra uno (2) para salir del programa\n");
    
      gotoxy(20, 10);
      printf("Su respuesta es : ");
    
      gotoxy(39, 10);
      scanf("\n\n%i", &choice);
      gotoxy(1, 1);
    
      switch (choice) {
      case 1:
        sorteo();
        break;
    
      case 2:
        system("cls");
        gotoxy(30, 4);
        printf("Adios, vuelva pronto!!\n\n\n\n\n\n");
        getch();
        exit(1);
      }
    
      system("cls");
      gotoxy(25, 5);
    
      char otra;
      while (otra != 'n') {
        system("cls");
        gotoxy(19, 5);
        printf("Desea volver a intentar otros numeros? [y/n]\n\n");
        gotoxy(65, 5);
        otra = getch();
        if (otra == 'n') {
          system("cls");
          gotoxy(30, 4);
          printf("Adios, vuelva pronto!!\n\n\n\n\n\n");
          Sleep(200);
          break;
        }
        else;
        sorteo();
      }
    }
    > Salem like i said it always prints same 3random nums sequence when i start the prog
    Then you need to add to the start of main
    srand( time(NULL) );
    This sets the initial seed for the random number generator.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    Salem help with this issue on graphics iam trying to make simple graphics just normal menu style lime borlnd turbo c but i cant get sdl to work or allegro so i prefer simple stuff for cmd prompt in windows i get tbe results using conio2 on dev c and codeblocks but i need too much code just to make a rectangle filled with blue and i know there must be better stuff out there to do this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 12-14-2012, 11:00 AM
  2. threads and Real-time processes
    By inhahe in forum Windows Programming
    Replies: 5
    Last Post: 05-14-2008, 04:17 PM
  3. Replies: 17
    Last Post: 07-20-2007, 09:25 PM
  4. Replies: 6
    Last Post: 01-09-2007, 04:12 PM

Tags for this Thread