String issue

This is a discussion on String issue within the C++ Programming forums, part of the General Programming Boards category; Hi I am having a problem with inserting a decimal point into a string. Okay here it goes, I am ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    3

    String issue

    Hi I am having a problem with inserting a decimal point into a string.

    Okay here it goes,

    I am writing to an LCD display and I am having trouble converting a number to be displayed. For example, I have an integer that ranges from 1 to 9999 I need to display this integer in string format with a decimal point before the last character so 1 would be ' '' '' ''.''1' and 9999 would be '9''9''9''.''9' . Currently I have code that takes the number to string format directly but how do I add the decimal point and spaces to the left of the string to make sure it is printed correctly?

    Thanks
    Chris

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you have four characters, and you want to turn it into five, you could do something like this:
    [1] -> [1]
    [2] -> [2]
    [3] -> [3]
    "." -> [4]
    [4] -> [5]
    But maybe I'm not understanding the question.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    If you already have the number in a std::string, then try something like this:

    Code:
    void addDec(std::string &s)
    {
        s.insert(s.length()-1, 1, '.');
    }
    If you're using a char * instead of std::string, the code would be somewhat similar.

    Edit: Fixed idiocy in code.
    Last edited by MacGyver; 07-15-2008 at 01:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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