Adding an integer to a string.

This is a discussion on Adding an integer to a string. within the C++ Programming forums, part of the General Programming Boards category; Hi, if I have a basic function, lets say sampleStr which contains some string. I have another, integer, function and ...

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

    Adding an integer to a string.

    Hi, if I have a basic function, lets say sampleStr which contains some string. I have another, integer, function and would like to add the result of that function, in integer format to the end of the string, then how can I do it.

    I have tried what I think i should do, but that only returns the relative character (ie I want 65 put in, but 'A' is put in instead.

    Thanks in advance,
    mintsmike

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Look up the stringstream (ostringstream in particular), within the standard <sstream> header.

    Alternatively, if you want to use the "C way", look up sprintf() in <stdio.h> or <cstdio>.

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Yes, you need something that translates a number into a string of characters that represent that number. Stringstreams will work.
    Code:
    #include <cstdlib> //rand()
    #include <string>
    #include <sstream>
    #include <iostream>
    
    int main()
    {
       std::string initial("This number was generated randomly: ");
       std::ostringstream oss;
       oss << std::rand();
       initial += oss.str();
       std::cout << initial << std::endl;
    }
    EDIT: of course, this program prints number that's random until you look at it. Then it won't seem so random when the you it again.
    Last edited by CodeMonkey; 03-27-2009 at 04:12 PM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    It's too bad that operator << always returns an ostream&. If somehow it remembered what type was involved we could:
    Code:
    cout << (ostringstream() << number << date << string << 'a').str() << endl;
    Maybe in another lifetime.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    cout << (ostringstream() << number << date << string << 'a').str() << endl;
    What?

    Not much different from:
    Code:
    std::ostringstream ostrm;
    ostrm << number << date << string << 'a';
    std::cout << ostrm.str().c_str() << std::endl;

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Two lines different.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Quote Originally Posted by CodeMonkey View Post
    Two lines different.
    In labouring to be concise, your code eventually becomes obscure or unmaintainable.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,188
    Code:
    cout << (ostringstream() << number << date << string << 'a').str() << endl;
    O_o

    Code:
    cout << number << date << string << 'a' << endl;
    Soma

  9. #9
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Well there's the coup de grāce.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Quote Originally Posted by grumpy View Post
    In labouring to be concise, your code eventually becomes obscure or unmaintainable.
    But you might want something that turns an integer into a string with only one line, ala boost::lexical_cast. That's not very obscure.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Adding an integer to the end of a string
    By virx61 in forum C++ Programming
    Replies: 14
    Last Post: 03-12-2006, 11:29 AM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

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