S
SneakyElf
Hi all,
so i need to write a function called getString() that has a local char
array of 80 elements. function should ask the user to enter a
sentence, and store the sentence in the array. then function should
dynamically allocate a char array just large enough to hold the
sentence, plus the null terminator. the function then will copy the
sentence to the dynamically allocated array, and return a pointer to
the array.
Here's what I have, but I get all kinds of errors.
help?
void getString (char str1 [], char str2 []) {
int length = 80;
char myStr [length];
cout << "Please enter a sentence: " << endl;
cin. getline (myStr, length);
int index = 0;
char *newArray;
newArray = new char [length];
while (myStr [index] != '\0') {
newArray [index] = myStr [index];
index ++;
}
newArray [index] = '\0';
}
so i need to write a function called getString() that has a local char
array of 80 elements. function should ask the user to enter a
sentence, and store the sentence in the array. then function should
dynamically allocate a char array just large enough to hold the
sentence, plus the null terminator. the function then will copy the
sentence to the dynamically allocated array, and return a pointer to
the array.
Here's what I have, but I get all kinds of errors.
help?
void getString (char str1 [], char str2 []) {
int length = 80;
char myStr [length];
cout << "Please enter a sentence: " << endl;
cin. getline (myStr, length);
int index = 0;
char *newArray;
newArray = new char [length];
while (myStr [index] != '\0') {
newArray [index] = myStr [index];
index ++;
}
newArray [index] = '\0';
}