hay i really need some help here.First i need to know how to have a live chat with someone. Can someone please give me the direction on how to do that?

Recommended Answers

All 10 Replies

I am trying to store a string into an array. I have to read the string from a file, then store some charaters into an array. I used the getline(inFile list) to input the file. I am also using a substr(0, 6) for the first six charater. How do i store the first six charaters into a string array?

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 into sixCharString one at a time accesses each char using the 2 dimensional [][] syntax and then add a null terminating char to sixCharString at sixCharString[6].

Alternatively you could try this:
sixCharString = originalString.substr(0, 6);

i do not know anything about vectors.

so i should have a string symbolLabel[6] to store the substring with six charaters?

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<TypeToStore> 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.

let's work with what i know for now. this is how i read the file in and get the substring.
Then i try to store the substring into a string array. This is what it looks like. will that work?

getline(inFile, list);
list.substr(0, 6);
symbolLabel[100] = list.substr(0, 6);

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.

we arent a compiler. (although i can tell if that will work/wont work)...

take that code and put it in a test application to see if it works before just asking us.

this is the code is I just try but am i am getting an error.

const int label = 6;
string symbolLabel[label]
while (getline(inFile, list))
{
list.substr(0, 6);
}
for (int i = 0; i <= label; i++)
{
symbolLabel = list.substr(0, 6);
cout << symbolLabel << endl;
}

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 = list.substr(0, 6);
cout << symbolLabel << 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.