i am having a problem with a function i am writing that takes in a txt file and stores the data in 3 arrays. The data file is always rows containing three numbers such as

1 2 3
2 3 4
5 6 7
8 9 10


with each column being specific to an array. The problem i am having is that when the file is being assigned to the arrays in a for loop it always skips the 1,5,9, etc number. This causes damage to the output because it offsets the array assignments so they dont have the right numbers in them. My code is posted below and im not sure if it is a problem with the loop i wrote, or if it is a problem with the way the file is being read. I just need someone to push me in the right direction on this because i have been working on it for hours.

Thanks

int ReadFile(char fname[], double prin[], double rate[], double pay[])
	{
		int c, d;
		
		cout<<"\nEnter the name of the data file: ";
			cin>>fname;
	
		ifstream inFile(fname);
		for(c=0;c<=100;c++)
		{
			if(!(inFile>>fname))
			{
				break;
			}
			inFile>>prin[c]>>rate[c]>>pay[c];
			
		}
		
		inFile.close();
		
		
		for(d=0;d<(c+1);d++)
		{
			
			cout<<"Record "<<(d+1)<<": "<<prin[d]<<" "<<rate[d]<<" "<<pay[d]<<"\n";
		}

		return (d);
	}
for(c=0;c<=100;c++)
{
  if(!(inFile>>fname))
  {
    break;
  }
  inFile>>prin[c]>>rate[c]>>pay[c];
}

That looks suspicious. If the file only consists of rows with three numbers then trying to read something into fname will eat the first and every fourth number. Try this instead:

for (c = 0; c < 100; c++) {
  if (!(infile>>prin[c]>>rate[c]>>pay[c]))
    break;
}

I used c < 100 instead of c <= 100 because that tends to be a common off-by-one error. If your arrays are declared as "double array[100];" then you'll be accessing the 100th index of prin, rate, and pay, all of which are out of bounds.

hey thanks a lot...i had tried so many different things, and i dint know you could pass the arrays in that boolean expression.

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.