Thread: Problem in user conversion!!!

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    Unhappy Problem in user conversion!!!

    I wish to ask is there any method that can convert a user char input to integer?

    char input;

    cout<<"Please insert a number!!!";
    cin>>input;

    I wish to convert the user input to integer type because i want to do addition with it.

    Thanks for regards!!!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Why not read it as an int to begin with?

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    Wink reply

    Thanks for regards.

    I choose as char input because i want only one digit input. For integer, it'll be more than one digit and I'll face problem in my later part of my project...

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Subtract the character '0' from the char and you will get the int it represents:
    Code:
    int i = input - '0';

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int atoi ( const char * str );

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    atoi converts strings (and I would use lexical_cast or a stringstream for that anyway). The OP wants to convert a character.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    However, you should check that '0' <= input and input <= '9' first, since the user can also enter a non-digit character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    I've read something from Borland C++ and I think this can help me but I can't figure out what it means. Is there anybody can explain to me(Only for the char to int type)?


    The result of the expression is the same type as that of the two operands.
    Methods used in standard arithmetic conversions
    Type Converts to Method

    char int Zero or sign-extended (depends on default char type)




    unsigned char int Zero-filled high byte (always)
    signed char int Sign-extended (always)
    short int Same value; sign extended
    unsigned short unsigned int Same value; zero filled
    enum int Same value

    Special char, int, and enum conversions
    Note: The conversions discussed in this section are specific to Borland C++.
    Assigning a signed character object (such as a variable) to an integral object results in automatic sign extension. Objects of type signed char always use sign extension; objects of type unsigned char always set the high byte to zero when converted to int.
    Converting a longer integral type to a shorter type truncates the higher order bits and leaves low-order bits unchanged. Converting a shorter integral type to a longer type either sign-extends or zero-fills the extra bits of the new value, depending on whether the shorter type is signed or unsigned, respectively.
    Last edited by jeiwong; 10-18-2007 at 08:07 PM.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Are you having trouble with the solution given? It is really rather simple. The text you quoted doesn't seem to be necessary to understand to accomplish this task.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  3. problem with metric to english conversion
    By james ging in forum C Programming
    Replies: 4
    Last Post: 11-06-2003, 01:46 AM
  4. Problem with user count on board?
    By biosninja in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-13-2002, 05:49 AM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM