943,957 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1369
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 20th, 2008
0

String Concatetation

Expand Post »
I have variables a, b, c = 1, 2, 3.

I would like to make it a string abc.

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 31
Solved Threads: 10
Practically a Master Poster
OmniX is offline Offline
652 posts
since Dec 2007
Aug 20th, 2008
0

Re: String Concatetation

Use std::ostringstream class (from <sstream> header) and its member function str().
It's so simple ..
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Aug 20th, 2008
1

Re: String Concatetation

To be a bit clearer, you can use the stringstream class like so:
c++ Syntax (Toggle Plain Text)
  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/
Reputation Points: 33
Solved Threads: 18
Junior Poster in Training
mahlerfive is offline Offline
77 posts
since Aug 2008
Aug 20th, 2008
0

Re: String Concatetation

mahlerfive thankyou very much for your help, much appericated.

Regards X
Reputation Points: 31
Solved Threads: 10
Practically a Master Poster
OmniX is offline Offline
652 posts
since Dec 2007
Aug 20th, 2008
0

Re: String Concatetation

This isn't very related to the question, but mahlerfive, here is a few reasons why not to use system("pause") .
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Aug 20th, 2008
0

Re: String Concatetation

On the topic of pause, you got one for cls?

Thanks, Regards X
Reputation Points: 31
Solved Threads: 10
Practically a Master Poster
OmniX is offline Offline
652 posts
since Dec 2007
Aug 20th, 2008
0

Re: String Concatetation

Nope sorry, but its possible to clear the console buffer without using a system call. You will just have to look it up.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Aug 20th, 2008
1

Re: String Concatetation

Click to Expand / Collapse  Quote originally posted by OmniX ...
On the topic of pause, you got one for cls?
Here are a few options. I prefer option 4 myself
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Aug 20th, 2008
0

Re: String Concatetation

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
Reputation Points: 31
Solved Threads: 10
Practically a Master Poster
OmniX is offline Offline
652 posts
since Dec 2007
Aug 20th, 2008
0

Re: String Concatetation

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

Click to Expand / Collapse  Quote originally posted by OmniX ...
system("pause")
we should use cin.get()?
sort of. Here's another link for you
Last edited by Nick Evan; Aug 20th, 2008 at 11:56 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: [Linker error] undefined reference to `D3DXCreateFontA@48'
Next Thread in C++ Forum Timeline: TAPI





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC