I need to convert a char to an int...

This is a discussion on I need to convert a char to an int... within the C++ Programming forums, part of the General Programming Boards category; Any advice would be greatly appreciated. I am doing something to this effect: cin.getline(input, 30, '\n'); I then break up ...

  1. #1
    IrishSlasher
    Guest

    I need to convert a char to an int...

    Any advice would be greatly appreciated.

    I am doing something to this effect:

    cin.getline(input, 30, '\n');

    I then break up the input to consecutive words
    ex: wait 5
    word1 will store wait
    word2 will store 5

    but, I need some way to convert 5 in word2 to an int to pass to another function that increments a clock.

    Just a simple conversion will do... Thank you anyone and everyone

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    70
    Didn't you hear about atoi ?

    It's ANSI.


    #include <stdlib.h>

    int atoi( const char *string );

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Actually it's a char array, not just a char .

    Check out the function atoi() (array-to-integer).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by golem
    Didn't you hear about atoi ?

    It's ANSI.


    #include <stdlib.h>

    int atoi( const char *string );
    so what? its about the worst way to do things. look up strtol().
    hello, internet!

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Hi,
    what's
    Code:
    strtol();
    and in what headerfile is it?
    I've never heared about it.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by moi
    so what? its about the worst way to do things. look up strtol().
    Why is that? Sure, you can specify a custom radix in strtol, but it is less portable than atoi. Can you give some arguments why you think so? Just blaming a function without telling why is bad habit.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    70

    strtol vs atoi

    Atoi is not more portable than strtol. Both are ANSI but atoi is easier to use, so I chose that one. Isn't it ridiculous to argue about such stupid things?

  8. #8
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    This is a C++ forum, use stringstream. It is superior.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  9. #9
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Magos
    Why is that? Sure, you can specify a custom radix in strtol, but it is less portable than atoi. Can you give some arguments why you think so? Just blaming a function without telling why is bad habit.
    strtol is no more or less portable than atoi. and atoi has no sort of error handling ability if letters or whatever are sent to it and not numbers.
    hello, internet!

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by moi
    strtol is no more or less portable than atoi.
    According to Borland's help files, the portability of strtol() doesn't include Unix, which atoi() does.
    I don't know anything about the error handling, but the function runs faster without it and if the user is dumb enough trying to convert "abc" to an integer, it's his fault .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ouch to magos

    P.S.
    vVv, what does the code in your sig do? It looks evil, but I can't decipher it...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    atoi vs. strtol, here is the diff.

    [quote]
    Hi,
    what's


    code:--------------------------------------------------------------------------------
    strtol();
    --------------------------------------------------------------------------------

    and in what headerfile is it?
    I've never heared about it.
    [/qoute]

    atoi
    Convert strings to integer atoi.

    int atoi( const char *string );

    Routine Required Header Compatibility
    atoi <stdlib.h> ANSI, Win 95, Win NT

    vs.

    strtol
    Convert strings to a long-integer value.

    long strtol( const char *nptr, char **endptr, int base );

    Routine Required Header Compatibility
    strtol <stdlib.h> ANSI, Win 95, Win NT

    I hope this helps!

  13. #13
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Magos
    According to Borland's help files, the portability of strtol() doesn't include Unix, which atoi() does.
    I don't know anything about the error handling, but the function runs faster without it and if the user is dumb enough trying to convert "abc" to an integer, it's his fault .
    borlands help files are wrong. strtol is ansi C just as much as atoi. and your second statement is a mark of bad coding practice.
    hello, internet!

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Alright everybody, calm down. No need to fight over Borland's help files! Moi, vVv has already more than squished Magus' argument, which means that you don't need to squish it yourself. End of argument, Magus is wrong! Everybody stop posting about it!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #15
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Here's an example of turning a char type into an int type, but it's probably not what you expect.

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    int main()
    {
      char character = '5';
      int num = 0;
    
      num = static_cast<int>(character);
      cout << "Number: " << num << endl;
      return 0;
    }

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

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 06:34 AM

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