How to read in time format?

This is a discussion on How to read in time format? within the C Programming forums, part of the General Programming Boards category; Can someone help me? thanks.... I tried reading the things written on net, but than i realised that i dont ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    1

    Question How to read in time format?

    Can someone help me? thanks.... I tried reading the things written on net, but than i realised that i dont know or never seen ALOT of the commands written there. I'm learning very very basic c programming till this point of time. thanks for your help.

    I'm supposed to read in the time given by the user.
    There are 2 ways of doing it.
    1) using the 12hour clock
    2) using the 24 hour clock

    To read in the 12 hour clock i use this
    printf ("Please enter time of entry: ");
    fflush (stdin);
    scanf ("%f%s",&two->oneone.entry_times,&two->oneone.time);

    but when i use this to read in the 24 hour clock, it always waits till i key in a alphabet. How so i just read in the numbers?

    if i just use ("%f",&two->oneone.entery_times)
    than i cant enter the "am" and "pm" part.

    what to do?

    Is there anyway to do stimulation of a car moving using very basic c programming commands? (this one is just curious, want to know only)

    thanks for all you help...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > fflush (stdin);
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    > I'm supposed to read in the time given by the user.
    Basically, you read all input as a string, then parse that string to see what the user typed.
    Code:
    #include <stdio.h>
    int main ( ) {
      char buff[100];
      if ( fgets( buff, sizeof buff, stdin ) != NULL ) {
        int now;
        char ampm[3];
        if ( sscanf( buff, "%d%2s", &now, ampm ) == 2 ) {
          printf( "12 hr time = %d %s\n", now, ampm );
        } else if ( sscanf( buff, "%d", &now ) == 1 ) {
          printf( "24 hr time = %04d\n", now );
        } else {
          printf( "Huh?\n" );
        }
      }
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  2. Need help with time
    By Gong in forum C++ Programming
    Replies: 7
    Last Post: 01-11-2007, 01:43 PM
  3. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  4. inputting time in separate compilation
    By sameintheend01 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2003, 03:33 AM
  5. give user time to read messages
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 04-21-2002, 12:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21