A C++ Problem.

This is a discussion on A C++ Problem. within the C++ Programming forums, part of the General Programming Boards category; Hi folks, Currently, in my project, the user types in a string of characters to a character array. Some of ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 05: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, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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