I need to create a function that returns a char array. I am not sure how to do this. I designed a custom string class for school and one of the functions has to convert the string to a char array and return the char array. TIA
Edit:
This now works in a way. I still have a problem though:
Code:
char * swcString::swcC_Str()
{
char * charArray = new char[swcSize()+1];
for (int i=0; i <= swcSize(); i++)
{
if (i < swcSize())
{
charArray[i] = myString[i];
cout << "charArray[i] = " << charArray[i] << endl;
}
else
{
charArray[i] = '\n';
}
}
return charArray;
}
Everything here works fine. I get the string converted into a char array and even newline works but then there is a bunch of garbage and i dont know why. a sample output would be:
First Name: MyFirstName
Middle Name: MyMiddleName
Last Name: MyLastName
Your are MyFirstName MyMiddleName MyLastName!
Your first name is 11 characters long!
Your middle name is 12 characters long!
Your last name is 10 characters long!
MyFirstName MyMiddleName MyLastName is 35 characters long!
Charater 4 in your First name: i
charArray[i] = M
charArray[i] = y
charArray[i] = F
charArray[i] = i
charArray[i] = r
charArray[i] = s
charArray[i] = t
charArray[i] = N
charArray[i] = a
charArray[i] = m
charArray[i] = e
First Name as char array: MyFirstName
²²²²▌▌▌▌▌▌▌▌A
Press any key to continue