C String/int question + Win/Unix Compile

Discussion in 'C Programming' started by ritchie, Feb 26, 2004.

  1. ritchie

    ritchie Guest

    Hello,

    Just wondering if anyone could help me with a basic C string-parsing
    question.

    I have an Integer date ie: YYYYMMDD.
    I can convert it into a string but I want to break it down to:
    Int DD, int MM, int YYYY.
    Is this possible?

    Also, I am using the getch function with in a windows app.
    This is just for a help function where a screen of info is displayed
    and then "press any key to continue...".
    It works fine but, I keep getting a warning of "'getch' undefined;
    assuming extern returning int".
    I compiled the same program on Solaris and got no warning for this.
    Should I be including another library for this?

    Thanks in advance,
    Ritchie
    ritchie, Feb 26, 2004
    #1
    1. Advertising

  2. ritchie <> scribbled the following:
    > Hello,


    > Just wondering if anyone could help me with a basic C string-parsing
    > question.


    > I have an Integer date ie: YYYYMMDD.
    > I can convert it into a string but I want to break it down to:
    > Int DD, int MM, int YYYY.
    > Is this possible?


    Yes it is. If you already have the YYYYMMDD representation in a string,
    sprintf() can be used to extract the DD, MM and YYYY parts. You don't
    specify how the integer value maps the date but I'm assuming it is
    10000*year + 100*month + day. Then you can use the % (modulus) and /
    (division) operators and sprintf() to extract the needed parts.

    > Also, I am using the getch function with in a windows app.
    > This is just for a help function where a screen of info is displayed
    > and then "press any key to continue...".
    > It works fine but, I keep getting a warning of "'getch' undefined;
    > assuming extern returning int".
    > I compiled the same program on Solaris and got no warning for this.
    > Should I be including another library for this?


    As getch() is a non-standard function, there is no standard answer. Your
    Windows or Solaris implementation might or might not supply it. Whether
    it does, and if it does, then what library it is in, is completely
    implementation-specific.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "I will never display my bum in public again."
    - Homer Simpson
    Joona I Palaste, Feb 26, 2004
    #2
    1. Advertising

  3. ritchie

    Mike Wahler Guest

    "ritchie" <> wrote in message
    news:...
    > Hello,
    >
    > Just wondering if anyone could help me with a basic C string-parsing
    > question.
    >
    > I have an Integer date ie: YYYYMMDD.


    There's no such type as 'Integer' in C.
    I'll assume you mean type 'int'.

    I'll also assume those letters represent decimal digits,
    e.g. 20040226. Note that on some platforms, this
    might be representable by type 'int' but its outside
    the required range of 'int' (-32767 to 32767). For
    maximal portablility, use 'long int' (or better yet,
    'unsigned long int')

    > I can convert it into a string but I want to break it down to:
    > Int DD, int MM, int YYYY.
    > Is this possible?


    As a number:
    (Using integer division):

    20040226 % 100 == 26
    20040226 / 100 = 200402

    200402 % 100 == 2
    200402 / 100 == 2004

    Get the idea?


    Or if you want to pick apart your string:

    char date[] = "20040226"
    char year[4];
    char day[2];
    char month[2];
    memcpy(year, date, 4);
    memcpy(day, date + 4, 2);
    memcpy(day, date + 6, 2);

    'memcpy()' is declared by <string.h>

    >
    > Also, I am using the getch function with in a windows app.


    There is no such function in standard C.

    > This is just for a help function where a screen of info is displayed
    > and then "press any key to continue...".


    Use the standard function 'getchar()'

    > It works fine but, I keep getting a warning of "'getch' undefined;
    > assuming extern returning int".


    Because there's apparently no declaration for it in scope.
    Perhaps your library doesn't have one, or you've forgotten
    to #include the required header.

    > I compiled the same program on Solaris and got no warning for this.


    C is a platform-independent language. If you stick with standard
    code, the platform should not matter (assuming you have a reasonably
    conforming C implementation).

    > Should I be including another library for this?


    The standard function 'getchar()' is declared by standard
    header <stdio.h>


    -Mike
    Mike Wahler, Feb 26, 2004
    #3
  4. ritchie

    Eric Sosman Guest

    ritchie wrote:
    >
    > Hello,
    >
    > Just wondering if anyone could help me with a basic C string-parsing
    > question.
    >
    > I have an Integer date ie: YYYYMMDD.
    > I can convert it into a string but I want to break it down to:
    > Int DD, int MM, int YYYY.
    > Is this possible?


    Yes, by using the / and % operators.

    > Also, I am using the getch function with in a windows app.
    > This is just for a help function where a screen of info is displayed
    > and then "press any key to continue...".
    > It works fine but, I keep getting a warning of "'getch' undefined;
    > assuming extern returning int".
    > I compiled the same program on Solaris and got no warning for this.
    > Should I be including another library for this?


    Yes: there is no getch() in the Standard C library,
    so if you want to use it you must get it from somewhere
    else.

    --
    Eric Sosman, Feb 26, 2004
    #4
  5. ritchie

    Malcolm Guest

    "Mike Wahler" <> wrote in message
    > >
    > > Also, I am using the getch function with in a windows app.

    >
    > There is no such function in standard C.
    >
    > > This is just for a help function where a screen of info is displayed
    > > and then "press any key to continue...".

    >
    > Use the standard function 'getchar()'
    >

    Unfortunately, on almost every implementation, characters don't become
    available on stdin until the user types return. So there is no portable
    solution.
    >
    > > I compiled the same program on Solaris and got no warning for
    > >this.

    >

    Sometimes non-standard functions are prototyped in standard headers. This is
    an undesirable and confusing practise.
    Malcolm, Feb 26, 2004
    #5
  6. ritchie

    Leor Zolman Guest

    On 26 Feb 2004 20:34:28 GMT, Joona I Palaste <>
    wrote:

    >ritchie <> scribbled the following:
    >> Hello,

    >
    >> Just wondering if anyone could help me with a basic C string-parsing
    >> question.

    >
    >> I have an Integer date ie: YYYYMMDD.
    >> I can convert it into a string but I want to break it down to:
    >> Int DD, int MM, int YYYY.
    >> Is this possible?

    >
    >Yes it is. If you already have the YYYYMMDD representation in a string,
    >sprintf() can be used to extract the DD, MM and YYYY parts.


    You meant sscanf(), of course.... e.g.:

    #include <stdio.h>

    int main(void)
    {
    char *date = "20040225";

    int year;
    int month;
    int day;

    if (sscanf(date, "%4d%2d%2d", &year, &month, &day) != 3)
    {
    printf("Conversion error.");
    }
    else
    {
    printf("year = %d, month = %d, day = %d\n",
    year, month, day);
    }
    return 0;
    }


    -leor

    >You don't
    >specify how the integer value maps the date but I'm assuming it is
    >10000*year + 100*month + day. Then you can use the % (modulus) and /
    >(division) operators and sprintf() to extract the needed parts.
    >
    >> Also, I am using the getch function with in a windows app.
    >> This is just for a help function where a screen of info is displayed
    >> and then "press any key to continue...".
    >> It works fine but, I keep getting a warning of "'getch' undefined;
    >> assuming extern returning int".
    >> I compiled the same program on Solaris and got no warning for this.
    >> Should I be including another library for this?

    >
    >As getch() is a non-standard function, there is no standard answer. Your
    >Windows or Solaris implementation might or might not supply it. Whether
    >it does, and if it does, then what library it is in, is completely
    >implementation-specific.


    Leor Zolman
    BD Software

    www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
    C++ users: Download BD Software's free STL Error Message
    Decryptor at www.bdsoft.com/tools/stlfilt.html
    Leor Zolman, Feb 26, 2004
    #6
  7. ritchie

    Mike Wahler Guest

    "Malcolm" <> wrote in message
    news:c1lm4a$2hq$...
    >
    > "Mike Wahler" <> wrote in message
    > > >
    > > > Also, I am using the getch function with in a windows app.

    > >
    > > There is no such function in standard C.
    > >
    > > > This is just for a help function where a screen of info is displayed
    > > > and then "press any key to continue...".

    > >
    > > Use the standard function 'getchar()'
    > >

    > Unfortunately, on almost every implementation, characters don't become
    > available on stdin until the user types return. So there is no portable
    > solution.


    Change the message to "Press return to continue". :)

    -Mike
    Mike Wahler, Feb 26, 2004
    #7
  8. ritchie

    Default User Guest

    ritchie wrote:
    >
    > Hello,
    >
    > Just wondering if anyone could help me with a basic C string-parsing
    > question.
    >
    > I have an Integer date ie: YYYYMMDD.
    > I can convert it into a string but I want to break it down to:
    > Int DD, int MM, int YYYY.
    > Is this possible?


    Sure. Depends on how you want to do it, but an easy way to to convert
    the number first, copy the first four digits to your "year" string, next
    two "month", etc.

    > Also, I am using the getch function with in a windows app.


    Non-standard function, off-topic here.

    > It works fine but, I keep getting a warning of "'getch' undefined;
    > assuming extern returning int".
    > I compiled the same program on Solaris and got no warning for this.
    > Should I be including another library for this?


    That means you are missing the header. If the library was missing, you'd
    get a link error. Find out which proprietary header defines that and add
    it in.



    Brian Rodenborn
    Default User, Feb 26, 2004
    #8
  9. "Mike Wahler" <> wrote in message
    news:lRs%b.9480$...
    >
    > "ritchie" <> wrote in message
    > news:...
    > > Hello,
    > >
    > > Just wondering if anyone could help me with a basic C string-parsing
    > > question.
    > >
    > > I have an Integer date ie: YYYYMMDD.

    ....
    > > I can convert it into a string but I want to break it down to:
    > > Int DD, int MM, int YYYY.
    > > Is this possible?

    ....
    > Or if you want to pick apart your string:
    >
    > char date[] = "20040226"
    > char year[4];
    > char day[2];
    > char month[2];
    > memcpy(year, date, 4);
    > memcpy(day, date + 4, 2);
    > memcpy(day, date + 6, 2);
    >
    > 'memcpy()' is declared by <string.h>


    Please note that Mike's example does not include terminating nulls, hence it
    does not produce valid C strings. Also, the second memcpy() should use month
    as the first parameter. Try:

    char date[] = "20040226"
    char year[5];
    char day[3];
    char month[3];
    strncpy(year, date, 4);
    strncpy(month, date + 4, 2);
    strncpy(day, date + 6, 2);
    year[4] = month[2] = day[2] = '\0';

    strncpy() is also declared in <string.h>

    I concur with the rest of Mike's post (the parts that I snipped).
    Peter Pichler, Feb 26, 2004
    #9
  10. ritchie

    ritchie Guest

    Hi,

    Thanks to all who replied.

    Have it working now>
    Iused the / and % operators to split the date.

    Thanks again,
    Ritchie

    Default User <> wrote in message news:<>...
    > ritchie wrote:
    > >
    > > Hello,
    > >
    > > Just wondering if anyone could help me with a basic C string-parsing
    > > question.
    > >
    > > I have an Integer date ie: YYYYMMDD.
    > > I can convert it into a string but I want to break it down to:
    > > Int DD, int MM, int YYYY.
    > > Is this possible?

    >
    > Sure. Depends on how you want to do it, but an easy way to to convert
    > the number first, copy the first four digits to your "year" string, next
    > two "month", etc.
    >
    > > Also, I am using the getch function with in a windows app.

    >
    > Non-standard function, off-topic here.
    >
    > > It works fine but, I keep getting a warning of "'getch' undefined;
    > > assuming extern returning int".
    > > I compiled the same program on Solaris and got no warning for this.
    > > Should I be including another library for this?

    >
    > That means you are missing the header. If the library was missing, you'd
    > get a link error. Find out which proprietary header defines that and add
    > it in.
    >
    >
    >
    > Brian Rodenborn
    ritchie, Feb 27, 2004
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    12
    Views:
    1,639
    Dave Thompson
    Jan 10, 2005
  2. Schnoffos
    Replies:
    2
    Views:
    1,212
    Martien Verbruggen
    Jun 27, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,634
    Old Wolf
    Jan 20, 2004
  4. Replies:
    18
    Views:
    619
    Dave Thompson
    Jan 10, 2005
  5. Krist
    Replies:
    6
    Views:
    741
    Arne Vajhøj
    May 7, 2010
Loading...

Share This Page