String Concatetation

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 609
Reputation: OmniX is an unknown quantity at this point 
Solved Threads: 8
OmniX's Avatar
OmniX OmniX is offline Offline
Practically a Master Poster

String Concatetation

 
0
  #1
Aug 20th, 2008
I have variables a, b, c = 1, 2, 3.

I would like to make it a string abc.

  1. int a = 1;
  2. int b = 2;
  3. int c = 3;
  4.  
  5. string abc = "";
  6.  
  7. abc = a + "" + b + "" + c ;
  8.  
  9. count << abc;

That above code does not work

Thanks, Regards X
"You never stop learning." - OmniX
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: String Concatetation

 
0
  #2
Aug 20th, 2008
Use std::ostringstream class (from <sstream> header) and its member function str().
It's so simple ..
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 77
Reputation: mahlerfive is an unknown quantity at this point 
Solved Threads: 16
mahlerfive mahlerfive is offline Offline
Junior Poster in Training

Re: String Concatetation

 
1
  #3
Aug 20th, 2008
To be a bit clearer, you can use the stringstream class like so:
  1. #include <iostream>
  2. #include <sstream>
  3. using namespace std;
  4.  
  5. int main() {
  6. stringstream ss; // our stringstream object
  7. int a = 1, b = 2, c = 3;
  8.  
  9. ss << a << b << c; // we insert a b and c into the stringstream
  10. cout << ss.str(); // call str() to get the string out
  11.  
  12. system("pause");
  13. return 0;
  14. }

For more info on stringstream check out: http://www.cplusplus.com/reference/i.../stringstream/
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 609
Reputation: OmniX is an unknown quantity at this point 
Solved Threads: 8
OmniX's Avatar
OmniX OmniX is offline Offline
Practically a Master Poster

Re: String Concatetation

 
0
  #4
Aug 20th, 2008
mahlerfive thankyou very much for your help, much appericated.

Regards X
"You never stop learning." - OmniX
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,431
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 118
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

Re: String Concatetation

 
0
  #5
Aug 20th, 2008
This isn't very related to the question, but mahlerfive, here is a few reasons why not to use system("pause") .
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 609
Reputation: OmniX is an unknown quantity at this point 
Solved Threads: 8
OmniX's Avatar
OmniX OmniX is offline Offline
Practically a Master Poster

Re: String Concatetation

 
0
  #6
Aug 20th, 2008
On the topic of pause, you got one for cls?

Thanks, Regards X
"You never stop learning." - OmniX
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,431
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 118
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

Re: String Concatetation

 
0
  #7
Aug 20th, 2008
Nope sorry, but its possible to clear the console buffer without using a system call. You will just have to look it up.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,864
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: String Concatetation

 
1
  #8
Aug 20th, 2008
Originally Posted by OmniX View Post
On the topic of pause, you got one for cls?
Here are a few options. I prefer option 4 myself
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 609
Reputation: OmniX is an unknown quantity at this point 
Solved Threads: 8
OmniX's Avatar
OmniX OmniX is offline Offline
Practically a Master Poster

Re: String Concatetation

 
0
  #9
Aug 20th, 2008
Hahahaha

I just pissed myself, im expecting serious input at option 4 and boom ahhh a good laugh.

But on a serious note, I liked option 1 - clear and simple.

Agree, disagree?

On a side note:

for
system("pause")
we should use cin.get()?

Or something like make a function an int and return 0, something like that but cins the way to go i gather

ahh good laugh off to bed goodnight people
"You never stop learning." - OmniX
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,864
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: String Concatetation

 
0
  #10
Aug 20th, 2008
I like option 1 in all it's simplicity, but it's main disadvantage is that the cursor is at the bottom of the screen after using this method. So the next time you write anything to the screen, it will be written at the bottom rather then the top.
I personally like (and use) the snippet from option 6

Originally Posted by OmniX View Post
system("pause")
we should use cin.get()?
sort of. Here's another link for you
Last edited by niek_e; Aug 20th, 2008 at 11:56 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC