Here is how to code that loop. You can delete lines 8, 10 and 19
int count = 0;
while( getline(fin, product[count]) )
count++;
Ancient Dragon
Retired & Loving It
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
There are two versions of getline() -- cin.getline() is for character arrays and getline(cin,...) if for std::strings.
You didn't say how the data file is formatted, so I just assumed it has all the information on one line because you said you wanted to use getline(). Post a few lines of the data file so that we can see how it should be read.
Ancient Dragon
Retired & Loving It
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
First I would create a structure (or class if you like) to contain all the information for one company. Change the function to accept a reference to a vector of structures instead of all those string arrays. That will simplify your intire program.
struct company
{
string companyName;
string something; // e.g. A123, whatever that represents
string somethingElse // e.g. case
// etc. etc for each of the other fields
};
// create an array to hold the structures
long readFile (vector<company>& colist)
{
// now read the data
struct company co;
while( getline(fin,co.company) )
{
getline(fin,co.something);
getline(fin,co.somethingElse);
// etc for each field
//
// finally put co into the vector
colist.push_back(co);
}
}
Ancient Dragon
Retired & Loving It
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
n8makar - congratulations on finding your own solution. I'm surprised the others who responded didn't spot that.
Mixing input with the extraction operator ( >> ) and getlline is a frequent source of problems for beginning students, that left over newline causes no end of frustration.
Now consider what happens if data is like:
12345 This is the name of the thing
8765 Some other thing
Your use of fin.ignore( ) will throw away the data we actually want.
An alternative means of removing the whitespace (blanks, tabs, newlines) is the ws manipulator. You might use it like:
fin >> num_var;
fin >> ws; //eats up whitspace till displayable char
getline( fin, string_var );
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228