I would like to ask a question about yielding the value of an object and assigning it to a string or a char* array.
Here's what I have so far...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int main() {
vector<string> str_vec;
string line;
cout << "Enter a string: ";
while(getline(cin, line)) {
str_vec.push_back(line);
cout << "Enter a string: ";
}
char **ch_arr = newchar* [str_vec.size()];
for(vector<string>::iterator i = str_vec.begin(); i != str_vec.end(); ++i) {
//I WANT TO ASSIGN THE RESULT OF *i TO THE 1ST POSITION
//OF ch_arr AND THEN TO THE 2nd AND SO ON
}
return 0;
}
I can't find the correct way to yield the value of i using the * operator, get the string object out and assign to the corresponding position of the character array ch_arr. How can I do this ?
*i gives you a string as you'd expect. The problem is you can't assign a string to a char array. You probably need to copy the string data with strcpy.
Just be sure your char array is large enough to hold the entire string data plus 1 (for the null terminator)
EDIT:
Also note, the string c_str() function gives you a pointer to the string data. You'll need to use this along with strcpy()