I am trying to combine the string NCards and Scards into one string called Tcards.
I am using # include <cstring> and I get an error. How would I fix this. All the variables are strings.
void deck::deal()
{
Tcards=strcpy(Scards, Ncards);
} I am trying to combine the string NCards and Scards into one string called Tcards.
I am using # include <cstring> and I get an error. How would I fix this. All the variables are strings.
void deck::deal()
{
Tcards=strcpy(Scards, Ncards);
} The root cause is almost always a type mismatch: confirm whether Scards, Ncards, and Tcards are C-style char arrays or std::string objects. said they were "strings" but the include and the error suggest a mismatch. and correctly pushed in the right direction — decide the type first, then pick the right approach.
If they are std::string, prefer C++ string operations. Reserving capacity before joining avoids extra reallocations on large strings:
std::string Tcards;
Tcards.reserve(Scards.size() + Ncards.size());
Tcards = Scards;
Tcards.append(Ncards); If they are C-style char* / char[], compute the exact buffer size (length1 + length2 + 1 for the terminator), allocate a buffer that size, and use a safe formatting function instead of unchecked copies:
size_t needed = std::strlen(Scards) + std::strlen(Ncards) + 1;
std::vector<char> Tcards(needed);
std::snprintf(Tcards.data(), Tcards.size(), "%s%s", Scards, Ncards); Notes and cautions: include <string> for std::string usage and <cstring> / <cstdio> for the C APIs. std::string::c_str() returns const char* (so it is not a writable destination); in C++17 data() can provide a mutable buffer, but modifying it manually is error-prone. Avoid raw strcpy/strcat without size checks; prefer snprintf, strn... variants, or better yet stick with std::string to eliminate manual memory management.
Jump to Post— jonsca 1,059
strcpy(dest,source)copies the source to the destination
EDIT:strcpyreturns the destination string, you're treating it like it returns a concatenated one.What you need is to
strcpyScards into Tcards and then usestrcatto add Ncards to Tcards. Make sure Tcards is big enough to take …
strcpy(dest,source) copies the source to the destination
EDIT: strcpy returns the destination string, you're treating it like it returns a concatenated one.
What you need is to strcpy Scards into Tcards and then use strcat to add Ncards to Tcards. Make sure Tcards is big enough to take on both strings.
Need more input. What is the declaration for the xCards? If they are Cstyle strings then you can't assign one to the other and if they are STL string objects then they won't work with strcpy().
For Cstyle strings there is a strcat() function that will concatenate one string onto the other, assuming the first string has enough space to accomate the second string. Then, once you have concatenated B onto A you can copy A into C with strcpy.
For STL string objects you can use the + operator to concatenate B onto A and then use the assingment operator to assign the new, modified and longer A to C.
Edit: Need to type faster.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.