Add int in strcpy()

Hi!

I'm pretty new to C++, and i got a basic question... Is there a way to put and int in a strcpy / strcat ?

1
2
3
4
5
6
7
8
9
10
char* Movie::toString()
{
	char* info = new char[100];

	strcpy(info, getTitle());	
	strcat(info, " ");
	strcat(info, getMovieID()); // Wont work

	return info;
}


the goal is: The function is suppose to return a string with all the variable of the class, but some are interger. I tried
strcat(info, (char*)getMovieID());
but it wont work either...
you can try memcpy instead. That will copy raw memory instead of checking for the terminating character \0.

memcpy(&dest, &source, sizeof(source));
I'm not sure I understand how it's suppose to fix the problem of the cast. In both way, the second parameter needed is a char, and i have a int...

I tried what what you said, but I got the same error.

Did I missed something?

Thanks!
In C++, there are better ways of handling strings. I suggest doing something like this:

1
2
3
4
5
6
7
#include <sstream>
//...
std::string Movie::toString() {
    std::stringstream ss;
    ss << getTitle() << " " << getMovieID();
    return ss.str();
}


If you have to return a char *, you will have to use strcpy (or equivelent) on ss.str().c_str(), which leaves deleting the memory up to the user. The above is much safer.

In C, you could use sprintf. The example here even does it:
http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/
Thanks! It worked really well with sstream! I did not need to have char* return type, i was just testing around.

Thanks again :)
Topic archived. No new replies allowed.