954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Files and Arrays

When I run this program I can only access the first row of data. Can any one help so that I can access all the lines? I am trying to store numbers into a 2-dimensional array.

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;


int main ()
{
ifstream inData;
float numbers[25][10];
int rows=0,cols;

inData.open("data.txt");

while (inData){
for (rows=0; rows<=7; rows++){
for (cols=0; cols<=7; cols++)
{
inData >> numbers[rows][cols];
cout<<fixed<<showpoint<<setprecision(1)<<numbers[rows][cols]<<" ";

}
cout<<endl;
}

}
cout<<endl;
cout<<fixed<<showpoint<<setprecision(1)<<numbers[0][1];

return 0;
}

JackDurden
Junior Poster in Training
92 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

How about this one? But this has the danget of writing outside the bounds of the array, so you need to add more checkiing.

rows = cols = 0;
while( infile >> numbers[rows][cols])
{
    cols++;
    if(cols > 7)
    {
        cols = 0;
        rows++;
    }
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

It looks like you have declared an array of 25 rows and 10 columns. I'd suggest you intialize the rows to their value(rows=25) and columns (cols=10). Try my example

for (i=0; i<=rows; i++)
   {
            for (j=0; j<=cols; cols++)
            { 
	         inData >> numbers[i][j];
	         cout<<fixed<<showpoint<<setprecision(1)<<numbers[i][j]<<" ";
	
            }
   }

dont forget to declare your array numbers[rows][cols]

joshmo
Posting Whiz in Training
280 posts since Oct 2007
Reputation Points: 19
Solved Threads: 20
 

That seems to have done it! Thanks for the help!

JackDurden
Junior Poster in Training
92 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

>> for (i=0; i<=rows; i++)

I hope you realize that is going to cause data overflow errors which might possibly crash your program or destroy other parts of your program if that code is incorporated into a larger program. Why? Because the loop performs one too many iterations -- if the value of rows is 25 then the above loop will count from 0 to 26, which is one too many. Same with the inner j loop.

What you want is this:
for (i=0; i < rows; i++)

Then there is the danger that the data file doesn't contain that many numbers (rows * columns).

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

>> for (i=0; i<=rows; i++)

I hope you realize that is going to cause data overflow errors which might possibly crash your program or destroy other parts of your program if that code is incorporated into a larger program. Why? Because the loop performs one too many iterations -- if the value of rows is 25 then the above loop will count from 0 to 26, which is one too many. Same with the inner j loop.

What you want is this: for (i=0; i < rows; i++)

Then there is the danger that the data file doesn't contain that many numbers (rows * columns).


Thanks for the correction. I happened to have edited the OP's code without putting in mind the the syntax...I meant to pass on the logic

joshmo
Posting Whiz in Training
280 posts since Oct 2007
Reputation Points: 19
Solved Threads: 20
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You