Hi! I have a tab delimited file that looks like this
NAME AGE HEIGHT WEIGHT REMARKS
Pupil1 15 176 190 overweight - diet plan
Pupil2 16 190 150 normal
Pupil3 15 187 165 Normal
How can I store the names and remarks in some sort of "array" for further reference? I can store the age, height and weight in arrays using int array[]. Also, I need to get the side comments in the remarks like in the first entry. Please help. Thanks in advance. :))
Rather than having multiple arrays you could define a struct to consist of
string name
int age
int height
int weight
string remarks
then just have a single array of this struct.
Since the fields are tab delimited, you could use getline with the tab character as a delimiter for the string fields. Remember the last field on the line is delimited by the newline character, rather than a tab. http://www.cplusplus.com/reference/string/string/getline/
The numeric fields could be read using the same method, where the value is first read as a string, then converted to an integer (for example by using a stringstream, or the atoi() function).
Example of using atoi here: http://www.cplusplus.com/forum/beginner/98960/#msg531953
Note, when you mix getline() and >> input operations, you need to remember that these behave slightly differently. getline will remove the delimiter, while >> leaves it remaining in the buffer.
"Some sort of array" depends on how you are going to access elements, whether the elements shall be ordered and so on.
If you need indeed something as array you can use std::vector.
You can define a record of the file as a structure as it was said already,
For example