Appended integer literal to std::string

This is a discussion on Appended integer literal to std::string within the C++ Programming forums, part of the General Programming Boards category; This question might have a very very simplistic answer, but it's something I've been wondering for awhile. I have say, ...

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    Appended integer literal to std::string

    This question might have a very very simplistic answer, but it's something I've been wondering for awhile. I have say, an error string that I'm trying to print, and I want to append say whatever GetLastError() retuned onto the error string, but it tries to interpret this as the ASCII code of a character, not what I want, etc. I would rather not have an intermediary buffer to do itoa() onto, and then append that string to the error string, but if that's really what you have to do....

  2. #2
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70
    The way I do it (and the way I saw it done) is to use string streams...

    Code:
    #include <sstream>
    blah blah blah;
    
    stringstream converter;
    converter << thenumber;  // tacks the number onto the stream
    string errnumber = converter.str();  // turn stream into a string
    string myimportantstring = errormessage + errnumber;  // append
    Probably not the most efficient, but it works and no other way has worked for me.

  3. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    You could use this function append_int:

    Code:
    #include <iostream>
    
    #include <string>
    
    void append_int(std::string & s, unsigned int n);
    
    int main()
    {
    	std::string x;
    
    	x = "Hello! ";
    
    	append_int(x, 25);
    
    	std::cout << x << std::endl;
    
    
    	return 0;
    }
    
    void append_int(std::string & s, unsigned int n)
    {
        unsigned int tmp = 0;
    
        while (n) {
           tmp *= 10;
           tmp += n % 10;
           n /= 10;
        }
    
        while (tmp) {
            s.push_back('0' + tmp % 10);
            tmp /= 10;
        }
    }

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {
    	int x = 10;
    	string s;
    	stringstream ss;
    	ss << x;
    	s = ss.str();
    	cout << s << endl;
    	cin.get();
    	return 0;
    }

    Just like nazca said only my way.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Ah, looking for resources for my other problem, I came across: http://rafb.net/efnet_cpp/faq/conversions/int2string/ which describes the stringstream technqiue (don't much want to use boost lib's or char*'s here). Anyway's, I like the append_int function a lot, thank you all for helping.

  6. #6
    Banned
    Join Date
    Jun 2005
    Posts
    594
    where do you see char* and what is boost lib?

    if by boost lib you mean non-standard library's,
    i havent used any non-standard library's.

    that code should be supported by and compiler
    on any system that is standard c++ compliant.
    Last edited by ILoveVectors; 07-30-2005 at 10:36 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Anyway's, I like the append_int function a lot
    You could use stringstreams to implement that append_int() function:
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    void append_int(std::string & s, unsigned int n);
    
    int main() {
    	std::string x("Hello! ");
    
    	append_int(x, 25);
    
    	std::cout << x << std::endl;
    
    	return 0;
    }
    
    void append_int(std::string & s, unsigned int n) {
    	std::stringstream ss;
    	ss << n;
    	s.append(ss.str());
    }
    what is boost lib
    Probably talking about boost.org
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  5. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 07:08 PM

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