declare an array (or vector) of strings to hold the original strings and a second array (or vector) to hold the 6 char strings. Use a loop to read the first 6 char of the originalString[i] into sixCharString[i] one at a time accesses each char using the 2 dimensional [][] syntax and then add a null terminating char to sixCharString[i] at sixCharString[i][6].
Alternatively you could try this:
sixCharString[i] = originalString[i].substr(0, 6);
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
i do not know anything about vectors.
That's okay. Arrays will work.
A brief explanation of vectors is that vectors are arrays that self expand as needed, unlike arrays that need to be hardcoded at compilation. Vectors work well if you like the syntax of arrays but don't know how many elements you will have and you don't want to use a list. The syntax is something like this:
vector v;
Now v will behave as an array but may hold anywhere from zero to however many elements are needed, as long as you have the memory to hold that many. There are some other nice features of vectors, too, when you start to use them, but for now, just think of them as self expanding arrays.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
If you put lines 1 and 3 in a loop and use an index variable instead of hardcoding 100 in line 3 it should work as long as everything is declared properly. You are skipping the step of storing the original input into an array as it's read from the file, but you will be storing the first 6 char of each line in the file in a array of strings. I don't have a compiler at this time to write a full program and test it, so I'll leave that up to you.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
const int label = 6;
string symbolLabel[label] //need a semicolon here
The above two lines create a array of strings that can hold up to 6 strings of any length desired. The any length desired part isn't really the problem here, however, I suspect you may want to hold more than 6 strings in the array and each string will be 6 alphabetical char long.
while (getline(inFile, list))
//this line reads whatever file inFile is associated with one line at a time and will place each line as it is read into a string called list overwriting the previous value each time through the loop. This is fine.
list.substr(0, 6);
//this line will create a temporary string consisting of the first 6 char of list, and then promptly ignore it
for (int i = 0; i <= label; i++)
{
symbolLabel[i] = list.substr(0, 6);
cout << symbolLabel[i] << endl;
}
//this will (probably) store the first six char of the last string read in into each of the 6 elements of symbolLabel and then print the first 6 char of the last line in the file 6 times.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396