I am working on another thread, but I encountered a problem just testing importing into an array. I use this code and it just hangs. I am stuck, please help.

FYI, the scores.txt is in the same location as the source code.

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main ( )
{

const int ROWLIST=10;
const int COLLIST=10;
int inputARRAY[ROWLIST][COLLIST]={0};

ifstream infile;

infile.open("scores.txt");

int row=0;
int col=0;
while( !infile.eof() )
{
for ( row=0; row < ROWLIST; row++)
for ( col=0; col < COLLIST; col++)
inputARRAY[row][col];
}

for ( int r=0; r< ROWLIST; r++)
for ( int c=0;c<COLLIST;c++)
cout << inputARRAY[r][c] << endl;

infile.close();

return 0;
}

Recommended Answers

All 3 Replies

inputARRAY[row][col];

Does not do anything. Nothing. Nada.

So, you never get to end of file, and thus your loop runs forever, or until you pull the plug, whichever comes first.

Please refer back to your lesson on how to read from a file.

And if this is the same problem, where each row of data may be a different length, you cannot just loop through reading a fixed number of times. This code is expecting exactly 100 data items - actually, multiple sets of 100 items. You don't need the while loop wrapping the for loops. Pick one type.

I see what you are saying, but in a simple world this code works fine. I just dont' get the code to dump into an array. Yes you are also correct the size of the row will vary.

// Program to test input file
// Filename : infile.cpp

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

using namespace std;

int main( )
{
	ifstream infile;
	int i;
	float f;
	string s;

	infile.open("input1.txt");

	infile >> i >> f >> s;

	cout << i << endl << f << endl << s << endl;

	infile.close();

	return 0;
}
inputARRAY[row][col];

Does not do anything. Nothing. Nada.

So, you never get to end of file, and thus your loop runs forever, or until you pull the plug, whichever comes first.

Please refer back to your lesson on how to read from a file.

And if this is the same problem, where each row of data may be a different length, you cannot just loop through reading a fixed number of times. This code is expecting exactly 100 data items - actually, multiple sets of 100 items. You don't need the while loop wrapping the for loops. Pick one type.

Member Avatar for jencas

Perhaps you should try something like:

infile >> inputARRAY[row][col];

but the whole constructiom seems rather odd to me. What if more data is in the input file than your array is able to hold? Why dontcha try something like std::vector of structs holding these i, f and s?

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.