Adding an integer to the end of a string

This is a discussion on Adding an integer to the end of a string within the C++ Programming forums, part of the General Programming Boards category; I am working on a function that should construct a long string full of regular characters and numbers separated by ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    Adding an integer to the end of a string

    I am working on a function that should construct a long string full of regular characters and numbers separated by white space. My problem is with adding an integer to the string as a character. I've tried to use strcat because it is the only way I know how, but to no avail. Searching hasn't turned up much, and what I have found hasn't made sense to me. Here's a sample of what I am trying:

    Code:
    strcat(*list, finder->title);
    strcat(*list, " ");
    strcat(*list, (char)finder->value[1]);
    '*list' is a pointer to the string, '*finder' is a pointer to a structure containing 'char title[8]' and 'int value[2]', both filled with a usable string or integers. It works without the third strcat, so I know that's where the problem is.

    How can I add an integer (converted into a char) onto the end of a string?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Since this is C++ use stringstreams:
    Code:
    #include <sstream>
    
    int Int = 12;
    float Float = 3.4f;
    std::stringstream Stream;
    Stream << "String" << Int << "Another string" << Float;
    
    std::string s = Stream.str(); //Get std::string
    const char* c = Stream.str().c_str(); //Get char pointer
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Code:
    strcat(list, finder->title);
    strcat(list, " ");
    sprintf(&list[strlen(list)], "%d", finder->value[1]);
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    But using that you need to know the size of list before making the call or allocate a rediculiously large buffer. Why not use standard C++ that doesn't need to make such assumptions?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Distributed Programming beyonddc's Avatar
    Join Date
    Dec 2001
    Location
    Mass
    Posts
    31
    Quote Originally Posted by Magos
    Since this is C++ use stringstreams:
    Code:
    #include <sstream>
    
    int Int = 12;
    float Float = 3.4f;
    std::stringstream Stream;
    Stream << "String" << Int << "Another string" << Float;
    
    std::string s = Stream.str(); //Get std::string
    const char* c = Stream.str().c_str(); //Get char pointer
    ostringstream would work also.
    -dc

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Quote Originally Posted by Magos
    But using that you need to know the size of list before making the call or allocate a rediculiously large buffer. Why not use standard C++ that doesn't need to make such assumptions?
    He's already using strcat(). Implication being list is a c-string -- already defined and allocated. and strlen(list) gives you the current 'size' (length of string).

    And a good programmer makes sure there's enough space before using strcat() so there should be no problem with the technique. It's been done for decades.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    Thanks for presenting two different methods to do this. I got stringstreams working in a test program, but sprintf causes some wierd characters instead of letters to be entered in the string and it changes the console font permanently so I have to restart it.

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
            int value = 7;
            char title[8] = "balance";
            char list[256];
    
            strcat(list, title);
            strcat(list, " ");
            sprintf(&list[strlen(list)], "%d", value);
    
            cout << list << endl;
    }
    This program outputs "@@" and then "balance", but in a font that looks like ncurses borders and such, and it changes the command prompt after the program is done. What am I doing wrong?

    If someone could point me to where I can read a basic explanation of printf and stringstreams that would be helpful. I like to understand the functions I use when I'm using them.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You never initialized list, so when you tried to add title to it (using strcat) it didn't know where the end of the list string was. It had garbage (uninitialized) data and looked for the null terminator. You can start list off with the value of "balance", then add to that. Or you can initialize list as an empty string.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Another possibility:
    Code:
    #include <iostream>
    #include <cstdio>
    using namespace std;
    int main()
    {
       int value = 7;
       char title[] = "balance";
       char list[256];
       sprintf(list, "%s %d", title, value);
       cout << list << endl;
       return 0;
    }
    
    /* my output
    balance 7
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    strcat(list, title);
    When you first strcat it assumes there's a valid NULL-terminated C-string in list, which there isn't. It's just pure luck such a character appears at position 8 (7,9, or whatever) otherwise it might've crashed your program.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    Quote Originally Posted by Daved
    Or you can initialize list as an empty string.
    I'm still quite new at c++ and I'm already trying to do pretty complicated things. I forgot how to initialize arrays and strings. Is it

    Code:
    "char list[256] = "";"?
    also

    Quote Originally Posted by Dave_Sinkula
    sprintf(list, "%s %d", title, value);
    The formatting sprintf is capable of makes it ideal for use here. The lines of code needed will be 1 instead of 3 or 4.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Yes, your initialization of list to empty is fine, although it isn't necessary with Dave_Sinkula's solution. The strcat requires it because strcat uses the initial value of list, whereas sprintf doesn't, it just overwrites whatever was there.

    >> The lines of code needed will be 1 instead of 3 or 4.
    While sprintf is a good solution if you are using C style strings, the number of lines of code should be way down on your list of reasons for choosing a particular method, and then only as an indicator of clear code.

    BTW, if you are new to C++ you should really be using the C++ string class. There are plenty of tutorials and references on it available.

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    I've been working on the knowledge I gained from the tutorials, so that'd be why I haven't been using the c++ string class. Thanks for bringing it to my attention.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Unfortunately many tutorials and books (and college classes) simply aren't up to date enough to teach modern C++. The string class is part of the language standard and has been only since 1998, so while you should prefer to use it over C style strings, you will have to make extra effort to find the right materials that teach it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 11:32 AM
  4. Adding To The Middle Of A Linked List
    By LostNotFound in forum C++ Programming
    Replies: 1
    Last Post: 02-23-2003, 06:02 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 10:37 PM

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