Thread: A C++ Problem.

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    45

    A C++ Problem.

    Hi folks,

    Currently, in my project, the user types in a string of characters to a character array. Some of those characters are numbers that are vital to the running of the program. I have to include these numbers in int variables. I am currently having trouble, as 0 is transformed into 48 when I try to declare it as int. Similarly 9 turns into 57 and so on.

    Is there a way that I can retain the true value when I do what i described above. Thanks in advance.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Quote Originally Posted by mintsmike View Post
    Hi folks,

    Currently, in my project, the user types in a string of characters to a character array. Some of those characters are numbers that are vital to the running of the program. I have to include these numbers in int variables. I am currently having trouble, as 0 is transformed into 48 when I try to declare it as int. Similarly 9 turns into 57 and so on.

    Is there a way that I can retain the true value when I do what i described above. Thanks in advance.
    why do you use character array instead of std::string?

    to convert characters '0' - '9' into integers 0 - 9 use

    Code:
    char ch = '3';
    int i = ch - '0';
    to convert strings like "123" into integers use stringstream object
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    Ė David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM