sort of a noob as i have been doing c++ for only a couple months now. my problem is that i am using a function to pass data from a file into an array then into a .out file. the function im using to read the data from the file is using a while loop. the data file consists of the name of a store, then the pattern of a product code then product name then product price repeated over and over for multiple products. the product name has embedded blanks which i try to read with getline but fail horribly. no compiler error just debug assertion failure. using vs. can provide more info if needed. dont think you need main or other functions.function code for reading data is as follows:

long readFile (string code[], string product[], double price[])
{
	string temp,
		   store;
	long   count;
	ifstream fin ("program5.txt");

	getline(fin,store);
	count=0;
	fin>>temp;

	while (!fin.eof())
	{
        code[count]=temp;
		getline(fin,product[count]);
		fin>>price[count];
		
		count++;
        fin>>temp;
    }

	fin.close();
	return count;
}

ok that helps but i need more than just the product name. i need the product codes and prices to be put into arrays as well thus making 3 arrays.

I think it should be cin.getline...

tried it. first it wouldnt let me convert from string to char. also even if it did i think cin.getline would ask the user to input data which isnt what we want. it needs to be read from the data file.

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.

This is the text file. ignore spelling mistakes as I had to make all product names one word.

Chuck's Computer Company
A123
Case
59.99
B234
Motherboard
149.99
C345
Processor
199.99
D456
Keyboard
14.99
E567
Monitor
39.99
F789
Mouse
4.99
G890
Harddrive
9.99

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);
    }
}

looks good thank you for your time and quick replies! also learned a little something about cin.getline...

ok since im a beginner this was explained to me in a different way that was easier to understand. not bashing anyone else's solution, but this fix requires only one line of code. im just posting this in case someone else has a similar problem. apparently after reading the product code (the A123) a new line was trying to be read by the getline. therefore the only thing needed was:

fin.ignore(80, '\n');

right before the getline trying to read the product name. a simple fix.

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 );
This article has been dead for over six months. Start a new discussion instead.