Guys I'm supposed to write a program that displays the date and time using C along with username and password inputs

I came up with the following code

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <time.h>
#include <curses.h>

#define PASS_LEN 25

int current_getch;
int doloop = 1;
static WINDOW *mainwnd;
static WINDOW *screen;
WINDOW *my_win;

int now_sec, now_min, now_hour, now_day, now_wday, now_month, now_year;
time_t now;
struct tm *now_tm;

  void screen_init(void)
  {
   mainwnd = initscr();
   noecho();
   cbreak();
   nodelay(mainwnd, TRUE);
   refresh(); // 1)
   wrefresh(mainwnd);
   screen = newwin(20, 45, 5, 5);
   box(screen, ACS_VLINE, ACS_HLINE);
  }

  static void update_display(void)
 {
    curs_set(0);
    mvwprintw(screen,2,6,"%d:%d:%d",now_hour, now_min, now_sec);
    mvwprintw(screen,2,18,"%d-%d-%d", now_day, now_month, now_year);
    mvwprintw(screen,4,6,"USERNAME:");
    mvwprintw(screen,6,6,"PASSWORD:");
    mvwprintw(screen,10,15,"Press E to END");
    wrefresh(screen);
    refresh();
 }

  void maketime(void)

 {
     // Get the current date/time
     now = time(NULL);
     now_tm = localtime(&now);
     now_sec = now_tm->tm_sec;
     now_min = now_tm->tm_min;
     now_hour = now_tm->tm_hour;
     now_day = now_tm->tm_mday;
     now_wday = now_tm->tm_wday;
     now_month = now_tm->tm_mon + 1;
     now_year = now_tm->tm_year + 1900;

   }

  void screen_end(void)
  {
   endwin();
  }

  int main(void)
  {
    char user[256];
    char password[PASS_LEN];
    char *corr_password="letmein";
    int i=0;
    initscr();
    screen_init();

    while(doloop)
      {
       current_getch=getch();
       if(current_getch == 69)
       {
        doloop=0;
       }
       maketime();
       update_display();
      }

       move(10,13);
       printw("Username:");
       echo();
       cbreak();

     getstr(user);
       refresh();
       sleep(2);

       nocbreak();

       screen_end();
       printf("program ended successfully\n");
       return 0;
  }

The above code displays the time and date alright but it doesnt take the input for the username from the user.The getstr() doesnt work.

can anyone help pls..

Thanks.

> if(current_getch == 69)
With better indentation, you would see that the code to read a username is OUTSIDE your while loop.

Hi,

Thanks.I came up with this program in response.

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <time.h>
#include <curses.h>

#define PASS_LEN 25

char password[64];
int current_getch;
int doloop = 1;
static WINDOW *mainwnd;
static WINDOW *screen;
WINDOW *my_win;

int now_sec, now_min, now_hour, now_day, now_wday, now_month, now_year;
time_t now;
struct tm *now_tm;

  void screen_init(void)
  {
   mainwnd = initscr();
   noecho();
   cbreak();
   nodelay(mainwnd, TRUE);
   refresh(); // 1)
   wrefresh(mainwnd);
   screen = newwin(20, 45, 5, 5);
   box(screen, ACS_VLINE, ACS_HLINE);
  }

  static void update_display(void)
 {
    curs_set(0);
    mvwprintw(screen,2,6,"%d:%d:%d",now_hour, now_min, now_sec);
    mvwprintw(screen,2,18,"%d-%d-%d", now_day, now_month, now_year);
    mvwprintw(screen,4,6,"USERNAME:");
    mvwprintw(screen,6,6,"PASSWORD:");
    mvwprintw(screen,10,15,"Press E to END");
    wrefresh(screen);
    refresh();
 }
void maketime(void)
  {
     // Get the current date/time
     now = time(NULL);
     now_tm = localtime(&now);
     now_sec = now_tm->tm_sec;
     now_min = now_tm->tm_min;
     now_hour = now_tm->tm_hour;
     now_day = now_tm->tm_mday;
     now_wday = now_tm->tm_wday;
     now_month = now_tm->tm_mon + 1;
     now_year = now_tm->tm_year + 1900;
  }

  void screen_end(void)
  {
    endwin();
  }

  int main(void)
  {
    char user[64];
    char password[PASS_LEN];
    char *corr_password="rakesh";
    int i=0;
    initscr();
    screen_init();
    while(doloop)
      {
       current_getch=getch();
       if(current_getch == 69)
       {
        doloop=0;
       }
       maketime();
       update_display();
       echo();
       mvwgetnstr(screen,4,18,user,12);
       refresh();
       mvwgetnstr(screen,6,18,password,8);

     }
     screen_end();
      printf("program ended successfully\n");
      return 0;
  }

This gets the username and password as input from the user.However,it has one problem.The clock function is executing but doesnt get updated on the screen unless an input is given for the username and password...I know the solution is threading....but I've got no prior experience related to threading and hence it would be nice if you could help me out with a couple of posts related to threads in C....or how this program can be solved using threading in C..

Thanks..
:)

> I know the solution is threading
Or you could use a curses input function which doesn't wait.
Use getch() and build the string yourself, one character at a time, IF a key happens to be pressed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.