2005 said:
Hi
I have
char String[] = "Time";
char temp[];
int slot;
slot = 1;
What I need is:= String[] = "Time1"
temp = (char)slot
strcat(String, temp);
Something is a miss - could someone fix it for me pls.
This is what std::string is good at.
The construct:
char String[] = "Time";
says to the compiler - "Allocate a char array just big enough to fit the
characters { 'T', 'i', 'm' , 'e', '\0' }". In fact, the construct below:
char String[] = { 'T', 'i', 'm' , 'e', '\0' };
means exactly the same thing as your construct above.
Which also means this:
char String[5] = { 'T', 'i', 'm' , 'e', '\0' };
because the compiler automatically fills in the size by counting the
number of initializers.
You can't add more storage to statically defined arrays, you need to use
dynamically defined arrays (like std::string and std::vector).
Also "char temp[]" does not do what you think it does, nor does
temp = (char) slot;
The easiest way to do this is:
#include <string>
#include <sstream>
// conversion to string helper
template <typename w_type>
inline std::string ToString(
const w_type & i_value
) {
std:

stringstream l_stream;
l_stream << i_value;
return l_stream.str();
}
const std::string String( "Time" );
int slot = 1;
std::string Time1( String + ToString( slot ) );
Time1 should contain "Time1".