Problem in user conversion!!!

This is a discussion on Problem in user conversion!!! within the C++ Programming forums, part of the General Programming Boards category; I wish to ask is there any method that can convert a user char input to integer? char input; cout<<"Please ...

  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,211
    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,344
    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,344
    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
    22,302
    However, you should check that '0' <= input and input <= '9' first, since the user can also enter a non-digit character.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 09:07 PM.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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, 06:49 AM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM

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