Thread: Appended integer literal to std::string

  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
    28,413
    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
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06: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, 10:43 AM
  5. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 07:08 PM