Thread: Adding an integer to a string.

  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,815
    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
    937
    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
    937
    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
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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
    937
    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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    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
    937
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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, 12:29 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM