help with this

This is a discussion on help with this within the C Programming forums, part of the General Programming Boards category; Originally Posted by Salem > The specifications state that it is 2 digit year, so changing it to 4 isn't ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    Quote Originally Posted by Salem
    > The specifications state that it is 2 digit year, so changing it to 4 isn't allowed.
    Then you need to add all that logic to your compare function

    Like
    1. convert both strings to integer
    2. if int value is > 50, add 1900 to it, else add 2000 to it
    3. compare both integer results.
    That is it, but this is the problem i originally had, not sure how to do it.

    Any suggestions, if i post the sort code & the compare code i have, will you be able to advise?

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Use strtol() to convert a string to an int
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #18
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Code:
    Convert a string to double (atof and _wtof), integer (atoi, _atoi64, _wtoi and _wtoi64), or long integer (atol and _wtol).
    
    double atof(
       const char *string 
    );
    double _wtof(
       const wchar_t *string 
    );
    int atoi(
       const char *string 
    );
    __int64 _atoi64(
       const char *string 
    );
    int _wtoi(
       const wchar_t *string 
    );
    __int64 _wtoi64(
       const wchar_t *string 
    );
    long atol(
       const char *string 
    );
    long _wtol(
       const wchar_t *string 
    );
    Parameters
    string 
    String to be converted. 
    Return Value
    Each function returns the double, int, __int64, or long value produced by interpreting the input characters as a number. The return value is 0 (for atoi, _atoi64, _wtoi, and _wtoi64), 0L (for atol and _wtol), or 0.0 (for atof and _wtof) if the input cannot be converted to a value of that type. The return value is undefined in case of overflow.
    
    Remarks
    These functions convert a character string to a double-precision, floating-point value (atof and _wtof), an integer value (atoi, _atoi64, _wtoi and _wtoi64), or a long integer value (atol and _wtol). 
    
    The input string is a sequence of characters that can be interpreted as a numerical value of the specified type. The output value is affected by the setting of the LC_NUMERIC category in the current locale. For more information on the LC_NUMERIC category, see setlocale. The function stops reading the input string at the first character that it cannot recognize as part of a number. This character may be the null character ('\0' or L'\0') terminating the string.
    
    The string argument to atof and _wtof has the following form:
    
    [whitespace] [sign] [digits] [.digits] [ {d | D | e | E }[sign]digits]
    
    A whitespace consists of space or tab characters, which are ignored; sign is either plus (+) or minus (  ); and digits are one or more decimal digits. If no digits appear before the decimal point, at least one must appear after the decimal point. The decimal digits may be followed by an exponent, which consists of an introductory letter (d, D, e, or E) and an optionally signed decimal integer.
    
    atoi, _atoi64, atol, _wtoi, _wtoi64 and _wtol do not recognize decimal points or exponents. The string argument for these functions has the form:
    
    [whitespace] [sign]digits
    
    where whitespace, sign, and digits are as described for atof and _wtof.
    
    Generic-Text Routine Mappings
    
    TCHAR.H routine  _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined 
    _tstof atof atof  _wtof  
    _tstoi atoi atoi _wtoi 
    _tstoi64 _atoi64 _atoi64 _wtoi64 
    _tstol atol atol _wtol 
    _ttoi atoi atoi _wtoi 
    _ttoi64 _atoi64 _atoi64 _wtoi64 
    _ttol atol atol _wtol 
    
    Requirements
    Routine Required header Compatibility 
    atof <math.h> and <stdlib.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP 
    atoi <stdlib.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP 
    _atoi64 <stdlib.h> Win 98, Win Me, Win NT, Win 2000, Win XP 
    atol <stdlib.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP 
    _wtof <stdlib.h> or <wchar.h> Win 98, Win Me, Win NT, Win 2000, Win XP 
    _wtoi <stdlib.h> or <wchar.h> Win 98, Win Me, Win NT, Win 2000, Win XP 
    _wtoi64 <stdlib.h> or <wchar.h> Win 98, Win Me, Win NT, Win 2000, Win XP 
    _wtol <stdlib.h> or <wchar.h> Win 98, Win Me, Win NT, Win 2000, Win XP 
    
    For additional compatibility information, see Compatibility in the Introduction.
    
    Libraries
    
    All versions of the C run-time libraries.
    
    Example
    /* ATOF.C: This program shows how numbers stored
     * as strings can be converted to numeric values
     * using the atof, atoi, and atol functions.
     */
    
    #include <stdlib.h>
    #include <stdio.h>
    
    int main( void )
    {
       char *s; double x; int i; long l;
    
       s = "  -2309.12E-15";    /* Test of atof */
       x = atof( s );
       printf( "atof test: ASCII string: %s\tfloat:  %e\n", s, x );
    
       s = "7.8912654773d210";  /* Test of atof */
       x = atof( s );
       printf( "atof test: ASCII string: %s\tfloat:  %e\n", s, x );
    
       s = "  -9885 pigs";      /* Test of atoi */
       i = atoi( s );
       printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );
    
       s = "98854 dollars";     /* Test of atol */
       l = atol( s );
       printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
       
       return 0;
    }
    
    Output
    atof test: ASCII string:   -2309.12E-15   float:  -2.309120e-012
    atof test: ASCII string: 7.8912654773d210   float:  7.891265e+210
    atoi test: ASCII string:   -9885 pigs      integer: -9885
    atol test: ASCII string: 98854 dollars      long: 98854
    Last edited by andyhunter; 01-30-2005 at 05:23 PM. Reason: Salem doesn't kick my ass.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  4. #19
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Alright ding me on that one quzah. Note I did not write or really even read the example at the bottom.(hell didn't even realize it was there) I copied this directly from my compilers help on atoi(). -- Go microsoft!!!
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

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