C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 9th, 2005, 06:32 PM
veraction veraction is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 5 veraction User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 34 m 8 sec
Reputation Power: 0
How to add an int to a std::string???

Say I have a string object and I want to add an integer to it, how would I do it?

Code:
     #include <string>
     #include <iostream>
     
    using namespace std;
    
     int main() {
     	int value = 5;
     	string s;
     	s = "The value is: ";
     	s += value;
     
  	cout << s << endl;
  
     return 0;
     }
     
     


I'd like it to output, "The value is: 5" -- however all it outputs is "The value is: ".

Please help.. this simple problem is driving me nucking futs.

Reply With Quote
  #2  
Old April 9th, 2005, 07:08 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 24 m 9 sec
Reputation Power: 13
I'm not sure if this is standard C++, but in C you can use the sprintff() function, ie:

sprintf(s,"%d",value)

if you're familiar with printf() that should make sense

Reply With Quote
  #3  
Old April 9th, 2005, 07:29 PM
veraction veraction is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 5 veraction User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 34 m 8 sec
Reputation Power: 0
doesn't compile. I'm using C++ and its the GNU compiler (i think)

Reply With Quote
  #4  
Old April 9th, 2005, 07:48 PM
veraction veraction is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 5 veraction User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 34 m 8 sec
Reputation Power: 0
Nevermind, I finally figured it out

A. Converting from int to string is a little more complicated than converting a string to int. There are no standard C++ functions for this, but there is a way to do it using standard stringstreams. The class stringstream is declared in header sstream and is used like this:

std::stringstream ss;
std::string str;
ss << 31337;
ss >> str;

Reply With Quote
  #5  
Old April 9th, 2005, 09:36 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 24 m 9 sec
Reputation Power: 13
Quote:
Originally Posted by veraction
Nevermind, I finally figured it out

A. Converting from int to string is a little more complicated than converting a string to int. There are no standard C++ functions for this, but there is a way to do it using standard stringstreams. The class stringstream is declared in header sstream and is used like this:

std::stringstream ss;
std::string str;
ss << 31337;
ss >> str;

actually, it would probably be fairly easy to manually convert an int to a string... you'd just need to set a loop and devide your int by different powers of 10, logging your results as you go.....

but then, that would be the "interesting" way of dong it

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > How to add an int to a std::string???


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap