read two words by one string...How?

How i can read two strings like one string?

this is the file
id     hours   gpa     major   department          college
1111    130    2.00    Biol    Biology             Science
5555    89     3.02    Comp    Computer Science    Science
1001    26     3.21    Engl    English             Art


you can see that department some contain one word like "biology" and other contain two words like "Computer Science"!!

how i can read it?
i do like that
1
2
3
infile >> info[n].id >> info[n].hrs >> info[n].gpa >> info[n].major;
getline(infile,info[n].department;
infile >> info[n].college;


it is not working properly!!!!
Last edited on
The simple solution would be to make your file look like this:
id     hours   gpa     major   department          college
1111    130    2.00    Biol    Biology             Science
5555    89     3.02    Comp    Computer_Science    Science
1001    26     3.21    Engl    English             Art

I mean, replace spaces inside a value for a specific field (major/dept/college etc) with underscore characters.

Another solution would be to make your file look like this:
id     hours   gpa     major   department          college
1111    130    2.00    Biol#Biology#Science#
5555    89     3.02    Comp#Computer Science#Science#
1001    26     3.21    Engl#English#Art#

And then use infile >> for ints and doubles, and getline(infile,info[n].department,'#') for strings
Last edited on
Are the columns in the data file tab separated? If they are, then you can distinguish between the columns regardless of spaces between words.

The getline function can take an optional delimiting character.
getline( f, s, '\t' );

See: http://cplusplus.com/reference/string/getline/
we are not allowed to write in the file any things and symbol like "#" or other!
we must use as it!
If the data columns line up, you could also read a line at a time and split the string at specific indexes.
It appears to me that your teacher is asking you to determine something that is non-deterministic.

1. The columns are not separated by TAB or COMMA characters (which is very odd for this kind of file).

2. The columns are not aligned ("hours" does not line-up with its column)

3. The only immediate thing left is to count the number of spaces between words, and group those with only one interleaving space character. This is a dangerous solution, simply because someone could very easily (and unknowningly!) reduce the number of spaces between columns to one space when they edit the file...

4. The only other solution is to have an a priori knowledge of the available departments. (That is, your program would have a list of department names which it can match against each line in order to split it up.)

You need to ask your professor for clarification:
1. Can't do.
2. Read column headers and get indices of begin of each column. Each column's data lies between the indices beginning the current and next column (or EOL).
3. Is counting spaces reasonable? (Is it what he wants? [It isn't safe.])
4. Does he want you to have a built-in list of department names and the like?

Let us know.
Topic archived. No new replies allowed.