hi!

i want to concatenate a string with an integer that's why i used this code :

++k;
char f[50];
sprintf(f,"%d",k);  
strcat ( f,"N");

but what i got is this ( for example :1N) and what i want is some thing like this : N1

Could you please help me with this

Thank you verry muuuuch

good night

Recommended Answers

All 3 Replies

You don't need the strcat() in this case, just sprintf():

sprintf(f, "N%d", k);

Cue everyone telling you to use std::string instead of C-style strings.

commented: 5 +0

Here is another way you can concatenate the string and int together.

// must include sstream library (#include <sstream>)

int m = 21;
stringstream joinedString;
joinedString << "I am " <<m<<" years old.";
string result = joinedString.str();
commented: 5 +0

Cue everyone telling you to use std::string instead of C-style strings.

Or at least, snprintf ;)

(If you are using the 2011 standards compiler)

commented: 5 +0
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.