-
For the record, here is the code that works with an array of char pointers (now free of all bugs -- I hope):
Code:
#include <iostream>
#include <string>
using namespace std;
struct stringStruct {
stringStruct() : arrayOfPointers(NULL) { initialize(); }
void initialize();
string* str;
string* anotherStr;
const char** arrayOfPointers;
};
void stringStruct::initialize() {
str = NULL;
anotherStr = NULL;
}
void doStuff();
int main() {
doStuff();
cout<< "\nHaha, you got to press Enter."<<endl;
cin.get();
return 0;
}
void doStuff() {
stringStruct object;
object.str = new string;
*object.str = "Yes, a string...\n";
*object.str += "Yes, another string...\n";
*object.str += "Yes, even another string...\n";
int sizeOfStr = object.str->size();
object.anotherStr = new string;
int sizeOfArrayOfPointers = 3;
object.arrayOfPointers = new const char*[sizeOfArrayOfPointers];
int i2 = 0;
for (int i = 0; i < sizeOfStr; i++) {
if (object.anotherStr == NULL) {
object.anotherStr = new string;
}
if (object.str->at(i2) == '\n') { //we're at a new-line character
i2++; //increment so as to skip the new-line character to the next character, which wont be a new-line character
}
if (i == sizeOfArrayOfPointers - 1) { //we're at the last element of the array of pointers
//continue looping through object.str...
while (object.str->at(i2) != '\n') {
*object.anotherStr += object.str->at(i2);
i2++;
}
object.arrayOfPointers[i] = object.anotherStr->data();
for (int i = 0; i < sizeOfArrayOfPointers; i++) {
cout<< object.arrayOfPointers[i] <<endl;
}
if (object.arrayOfPointers) {
delete [] object.arrayOfPointers;
}
if (object.anotherStr) {
delete object.anotherStr;
object.anotherStr = NULL;
}
return; //out of the function
}
while (object.str->at(i2) != '\n') { //iterate till the next new-line character
*object.anotherStr += object.str->at(i2); //add characters to the "anotherStr"
i2++;
}
object.arrayOfPointers[i] = object.anotherStr->data();
object.anotherStr = NULL; //reset this pointer
}
}
And here is the code with the vector that I ended up using in place of the array of pointers:
Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string *str = new string;
*str = "Yes, a string...\n";
*str += "Yes, another string...\n";
*str += "Yes, even another string...\n";
string* anotherStr = new string;
int sizeOfStr = str->size();
vector <string> aVectorOfStrings; //create a vector for storing individual strings
int i2 = 0;
for (int i = 0; i < sizeOfStr; i++) {
if (anotherStr == NULL) {
anotherStr = new string;
}
if (str->at(i2) == '\n') {
i2++; //skip past the new-line character
}
while (str->at(i2) != '\n') { //read up until the next new-line character
*anotherStr += str->at(i2); //add characters to the string
i2++;
}
aVectorOfStrings.push_back(*anotherStr); //add a new string to the vector of strings
delete anotherStr;
anotherStr = NULL; //reset the pointer
if (i == 2) { //we just added the last string to the vector...
break; //out of the loop
}
}
int sizeOfVector = aVectorOfStrings.size();
for (int i = 0; i < sizeOfVector; i++) {
cout<< aVectorOfStrings.at(i) <<endl;
}
cout<< "\nHaha, you got to press Enter..."<<endl;
cin.get(); //make the user press enter
return 0;
}
Both work as expected, but the vector method is obviously preferred.