converting a char to an int

This is a discussion on converting a char to an int within the C++ Programming forums, part of the General Programming Boards category; how do i convert a char to an int?...

  1. #1
    bulabruddah
    Guest

    converting a char to an int

    how do i convert a char to an int?

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    cast it.
    Code:
    char x;
    int y;
    y = (int)x;

  3. #3
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Have you tried itoa()?

    Code:
    char cNum[10];
    int iNum=4;
    itoa(iNum,cNum,10);
    //now cNum="4";
    **edit** you need to include stdlib.h to use itoa()
    Last edited by jdinger; 09-02-2002 at 08:12 PM.

  4. #4
    Hmm...
    Guest
    how would you convert a string to a float value?

  5. #5
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Use atof(). For more details check out MSDN.

  6. #6
    hmmm...
    Guest
    when i compile my program i get this error:

    error C2664: 'atof' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'

    it won't let me convert the string to a float.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Use atof().
    No, use strtod. The ato* functions are unsafe because they result in undefined behavior if the value is outside of the range for the particular type. You also can't determine how many characters of the original string were a part of the conversion. Both of these problems can be solved by using the strto* functions instead.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Hmm...
    Guest
    it also says this after the above error message:

    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

  9. #9
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    sorry from the original post I didn't realize you were using strings (since you said char). I thought you were using a c style char array.
    Last edited by jdinger; 09-02-2002 at 08:57 PM.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >error C2664: 'atof' : cannot convert parameter 1 from 'class std::basic_string<snip>' to 'const char *'
    This is because you are using a C++ string, not an array of char terminated by a nul character. For your code to work, you should convert the string to a C string:
    Code:
    atof ( my_string.c_str() );
    Once again, I recommend using strtod instead of atof for safety reasons.

    -Prelude
    My best code is written with the delete key.

  11. #11
    hmmm...
    Guest
    does the strtod function automatically stop reading when it reads a letter? how do i use the strtod function?

  12. #12
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    From MSDN:

    Convert strings to a double-precision value.

    double strtod( const char *nptr, char **endptr );

    double wcstod( const wchar_t *nptr, wchar_t **endptr );

    Each of these functions converts the input string nptr to a double

    Parameters

    nptr

    Null-terminated string to convert

    endptr

    Pointer to character that stops scan

    Remarks

    The strtod function converts nptr to a double-precision value. strtod stops reading the string nptr at the first character it cannot recognize as part of a number. This may be the terminating null character. wcstod is a wide-character version of strtod; its nptr argument is a wide-character string. Otherwise these functions behave identically

    ***the lowly Grayson_Peddie bows to the Code Goddess.
    thanks for the rebuke, Prelude. I didn't have any experience with strtod before (atof hadn't failed me (yet)), so I learned something new.**
    Last edited by jdinger; 09-02-2002 at 09:06 PM.

  13. #13
    hmm...
    Guest
    okay, so in order to not get the error message above when using the strtod function, i would need to write it like this:

    x = strtod( string.c_str() , &stopstring.c_str() ).;

    is this right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  2. Replies: 14
    Last Post: 06-28-2006, 02:58 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 10:16 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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