Hi!
Can anybody please help me to add many strings in one?
I tryied this
int a=4,b=5,c=6;
string d;
d = #a+" Hello "+#b+" bye "+c;

I want that d = 4 Hello 5 bye 6

Please help me!

Recommended Answers

All 6 Replies

you need to use std::stringstream class. It is very similar to fstream but ccreates the result in memory instead of in a disk file.

#include <sstream>
#include <string>
using namespace std;

int main()
{
    int a = 4, b = 5, c = 6;
    string d;
    stringstream str;
    str << a << " Hello " << b << " bye " << c;
    d = str.str();
}

What compiler use to compile your code, I cant compile with gcc

Compiling: C:\Documents and Settings\a\Desktop\str.cpp
C:\Documents and Settings\a\Desktop\str.cpp:2:2: invalid preprocessing directive #incluce
C:\Documents and Settings\a\Desktop\str.cpp:3: error: expected nested-name-specifier before "namespce"
C:\Documents and Settings\a\Desktop\str.cpp:3: error: `namespce' has not been declared
C:\Documents and Settings\a\Desktop\str.cpp:3: error: expected `;' before "std"
C:\Documents and Settings\a\Desktop\str.cpp:3: error: expected constructor, destructor, or type conversion before ';' token
C:\Documents and Settings\a\Desktop\str.cpp: In function `int main()':
C:\Documents and Settings\a\Desktop\str.cpp:8: error: `string' undeclared (first use this function)
C:\Documents and Settings\a\Desktop\str.cpp:8: error: (Each undeclared identifier is reported only once for each function it appears in.)
C:\Documents and Settings\a\Desktop\str.cpp:8: error: expected `;' before "d"
C:\Documents and Settings\a\Desktop\str.cpp:9: error: `stringstream' undeclared (first use this function)
C:\Documents and Settings\a\Desktop\str.cpp:9: error: expected `;' before "str"
C:\Documents and Settings\a\Desktop\str.cpp:10: error: `str' undeclared (first use this function)
C:\Documents and Settings\a\Desktop\str.cpp:11: error: `d' undeclared (first use this function)

Have you read your errors?
No 1: invalid preprocessing directive #incluce
Have you heard of INCLUCE? I haven't
No 2: expected nested-name-specifier before "namespce"
Have you heard of NAMESPCE? I haven't
No 3: namespce again, and so on...

yea, there were a couple typing errors which you will have to correct before compiling that code. I've corrected them in my previous post.

Can you please tell me what editor/compiler you use?!

VC++ 2008 Express. But you should be able to compile that with g++ or any other modern compiler on either *nix or MS-Windows since the code I posted is just standard c++ code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.