Im inputting from a file to a 2d array and i dont know how to stop it once it gets to the end of the file the code i have for it so far is

for(int i=0; i<records; i++)
	{
		for(int j=0; j<month; j++)
		{
			inFile >> monthArray[i][j];
		}
	}

month is const int 12
records is const in 10

for records i want it to be able to accept up to 10 but it could be less and i dont know how to control that


any idea would be awesome :)

Try something like

for(int i=0; i<records && inFile; i++)
		{
			for(int j=0; j<month && inFile; j++)
			{
				inFile >> monthArray[i][j];
			}
		}

compiles and runs but gives me rows of 0's for the lines after the file ends. Is there a way to make that not happen? so when it runs out input even though records is 10 it stops?

Try changing the code like

for(int i=0; i<records && inFile; i++)
		{
			for(int j=0; j<month; j++)
			{
				int ans = 0;
				inFile >> ans;
				
				if (!inFile) break;
			
				monthArray[i][j] = ans;
			}
		}

still no luck with that :/

maybe the whole program would help

basically i have a couple lines of code coming in and then it fills in the remainder of the ten rows with 0's which i dont want i just want the array to take in the few lines and stop

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

using namespace std;

const int records = 10;
const int month = 12;
ifstream inFile, inFile2;
ofstream outFile;
int monthArray[records][month];
int main()
{
	inFile.open("data01.dat");
	if(! inFile)
	{
		cout << "No such input/output file exsists";
	}

	for(int i=0; i<records && inFile; i++)
	{
		for(int j=0; j<month; j++)
		{
			int hold=0;
			inFile >> hold;

			if(!inFile) break;
			monthArray[i][j] = hold;
		}
	}
	




	for(int i=0; i<records; i++)
	{
		for(int j=0; j<month; j++)
		{
			cout <<  monthArray[i][j] << " ";
		}
		cout << endl;
	}




	return 0;
}

I just ran your program and it worked...What are you using for data in your data01.dat file?

20 24 32 35 40 50 50 45 40 35 30 25
30 38 40 45 50 60 60 55 45 40 35 32
60 60 55 50 45 40 40 40 45 50 55 60
45 45 40 30 25 20 15 20 25 30 40 45

Again, I ran the program you posted with the data you posted and it worked.

Here's my output...

20 24 32 35 40 50 50 45 40 35 30 25
30 38 40 45 50 60 60 55 45 40 35 32
60 60 55 50 45 40 40 40 45 50 55 60
45 45 40 30 25 20 15 20 25 30 40 45
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0

what about getting rid of the 0's is that possible that's what i have been asking?

Nothing in the code keeps track of how much REAL data you actually have. You have records of real data, so this will display 4 records of real data, then 6 records of garbage.

You'll want to set up a new variables and change the code so that it flags the record where you start getting garbage in line 28. Only display up to the record before that in line 37.

commented: patient and helpful +1

what about getting rid of the 0's is that possible that's what i have been asking?

I'm not sure what you mean by 'get rid of', your array is defined as a 10 x 12 array. If you require an array that is only known at run time them you'll have to explore the new and delete operators or possibly a vector.

alright thanks so much for your patience and help :)

Again, I ran the program you posted with the data you posted and it worked.

Here's my output...

20 24 32 35 40 50 50 45 40 35 30 25
30 38 40 45 50 60 60 55 45 40 35 32
60 60 55 50 45 40 40 40 45 50 55 60
45 45 40 30 25 20 15 20 25 30 40 45
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0

There's no guarantee you'll get zeroes. On your particular compiler, it must initialize to 0, but it's undefined behavior. Plus perhaps 0's are valid data. You need to flag the point where the file ends somehow.

if you dont mind how would you do that with a vector if it wouldn't take to much of your time?

if you dont mind how would you do that with a vector if it wouldn't take to much of your time?

You can use a vector if you want, but the array approach will work fine if you flag things correctly.

int numRecords = 0;
	for(int i=0; i<records && inFile; i++)
	{
		for(int j=0; j<month; j++)
		{
			int hold=0;
			inFile >> hold;
 
			if(!inFile) break;
			monthArray[i][j] = hold;
		}
                if(inFile)
                    numRecords++;
	}

By the end, numRecords will hold 4. Change your display code to use the numRecords variable.

If you take the vector approach, line 13 would change to a "push_back" command instead and your display code would use the vector' size instead of numRecords.

sweet thanks gonna try that out!

you're a lifesaver thanks to all who contributed

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.