943,729 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 709
  • C++ RSS
Jul 28th, 2008
0

Files and Arrays

Expand Post »
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;
}
Last edited by JackDurden; Jul 28th, 2008 at 10:19 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Jul 28th, 2008
0

Re: Files and Arrays

How about this one? But this has the danget of writing outside the bounds of the array, so you need to add more checkiing.
C++ Syntax (Toggle Plain Text)
  1. rows = cols = 0;
  2. while( infile >> numbers[rows][cols])
  3. {
  4. cols++;
  5. if(cols > 7)
  6. {
  7. cols = 0;
  8. rows++;
  9. }
  10. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Jul 29th, 2008
0

Re: Files and Arrays

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
C++ Syntax (Toggle Plain Text)
  1. for (i=0; i<=rows; i++)
  2. {
  3. for (j=0; j<=cols; cols++)
  4. {
  5. inData >> numbers[i][j];
  6. cout<<fixed<<showpoint<<setprecision(1)<<numbers[i][j]<<" ";
  7.  
  8. }
  9. }
dont forget to declare your array numbers[rows][cols]
Last edited by joshmo; Jul 29th, 2008 at 4:42 am.
Reputation Points: 19
Solved Threads: 20
Posting Whiz in Training
joshmo is offline Offline
280 posts
since Oct 2007
Jul 29th, 2008
0

Re: Files and Arrays

That seems to have done it! Thanks for the help!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Jul 29th, 2008
0

Re: Files and Arrays

>> 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).
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Jul 29th, 2008
0

Re: Files and Arrays

>> 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
Reputation Points: 19
Solved Threads: 20
Posting Whiz in Training
joshmo is offline Offline
280 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C to C++
Next Thread in C++ Forum Timeline: Graphics API's





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC