Conversion of character string to integer

This is a discussion on Conversion of character string to integer within the C++ Programming forums, part of the General Programming Boards category; How do I convert a character to an integer? For example if I have a character '1' how do I ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Conversion of character string to integer

    How do I convert a character to an integer? For example if I have a character '1' how do I convert it into an integer type one. There is a good news and bad news. The bad news is that my character array starts out with "non-integer" characters and "integer" characters that gets tokenized and pushed into a queue data structure. The good news is that I'll be able to check if each element is an integer is not by some sort of test.
    Code:
    queue <char> Q1;
    // after loading characters string, we need to see if each element is an integer
    
    t = Q1.front();
    if (t == integer)
    // do mathematical operation
    else {
    // do something else
    }
    So, my question is how would you convert that character into a mathematical element? Thank you.

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I don't know where you're going with that if statement, but it looks rather doomed to me.

    As for converting characters to numberals, there are two easy ways to do this. One is atoi(). The other makes use of characters' ASCII values:
    Code:
    char ch;
    int x;
    cout << "Enter a digit: ";
    cin >> ch;
    ch -= '0';
    x = ch;
    cout << x;
    There, try that. If I screwed up a bit, look up an ASCII table to get the ASCII value exactly right. I think the numbers go 0-9. This solution will only work for a single digit, though. For larger numbers, you have to parse through a string character by characer.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    If you're just using single characters (not strings), then it is rather simple:

    if('0' <= x && x <= '9') // Then x is an integer.

    Note, joshdick's solution works fine because '0' will convert to the proper integer value for you (60 something, I think), without having to know it or rely on it.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    I tend to use isalpha(string);

    not of isalpha will tell wheather a string forms a number...... or you can use atoi which will throw an exception in case it is not a string..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM

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