Jump to content
  • Advertisement
Sign in to follow this  
polisasimo

Loading a two dimensionial array?

This topic is 4822 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all I am trying o load a two demensional array with numbers stored in a text file. During this process I want to implement a line of code everytime a new line is read, and I am having problems with this code:
	int array[MAXCHARS][MAXCHARS];
	int position, temp;
	int colum = 0;
	int row = 0;
	ifstream ifile;
	ifile.open("somefile.txt");

	ifile >> temp;
	while(!ifile.eof())
	{			
		array[colum][row] = temp;
		cout << array[colum][row] << " ";
		colum ++;
		if(temp == '\n')
		{
			row++;
			cout << endl;
		}
		ifile >> temp;
		
	}


Thanks for any help.

Share this post


Link to post
Share on other sites
Advertisement
I was going to say that when you get to that newline, and increment your row, you probably want to reset your column index back to zero. However, in a 4x4 matrix for example, the 5th column of the 1st row is equal to the 1st column of the 2nd row anyway.

What exactly is the problem you're having? Is there anything being displayed incorrectly? Or are you overflowing the array itself?

Share this post


Link to post
Share on other sites
I think the information I am looking for is, without using some brand of getline process how can I set the if statement to execute everytime a new line of the text file starts?

right now the code sets only the first part of the array [0][0] and then moves on to [0][1] then it moves up [1][1], [2][1], [3][1] etc.


I would like the array to look as the file does:

4 5 6 9 7 8
4 5 6 7 5 5 22 3 6 4
45 6 5
44 6 9 8
5 6 9 7 8
56 9 7 8 4 5 3

where array[1][6] == 22

Share this post


Link to post
Share on other sites
When you increment row, dont you want to reset column? you'll get an array-index-out-of-bounds related error eventually..

im not sure if this will work, but try it:
int array[MAXCHARS][MAXCHARS];
int position, temp;
int colum = 0;
int row = 0;
ifstream ifile;
ifile.open("somefile.txt");

ifile >> temp;
while(!ifile.eof())
{
array[colum][row] = temp;
cout << array[colum][row] << " ";
colum ++;
if( ifile.peek() == '\n' ) { // istream::peek
row++;
cout << endl;
}
ifile >> temp;
}

Share this post


Link to post
Share on other sites

#include <vector>
#include <sstream>
#include <string>
#include <iostream>
using namespace std;
// ...
vector<vector<int> > data;
ifstream file("data.dat");
string line;
while (getline(file, line)) {
stringstream parser(line);
vector<int> parsedLine;
int item;
while (parser >> item) parsedLine.push_back(item);
data.push_back(parsedLine);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!