My numbers are not being read in to the two dimensional array correctly and i'm completely lost :confused:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

const int SIZE_MONTH=4;
const int SIZE_DAYS=31;

int main()
{
	float temperatures[SIZE_MONTH][SIZE_DAYS];

	string months[SIZE_MONTH]={"January","February","March","April"};
    
	ifstream inFile;
	
	inFile.open("temp1.dat");

	if (!inFile)
		cout<<"File not found.";
	else
	{	
		
		for (int i=0; i< SIZE_MONTH; i++)
		{ 
			for (int k=0; k<SIZE_DAYS; k++)
			{	 
				inFile>>temperatures[i][k];
				cout<<temperatures[i][k]<<" ";
			}
		}
		
		inFile.close();
		
		float sum=0.0;

		for (int k=0; k<SIZE_DAYS; k++)
		{
			sum+=temperatures[1][k];
		}

		float average=sum/28;
		
		float lowest=temperatures[0][0];

		string month;

		for (int i=0; i< SIZE_MONTH; i++)
		{
			for (int k=0; k<SIZE_DAYS; k++)
			{
				if(temperatures[i][k]<lowest)
				{
					lowest=temperatures[i][k];
					month=months[i];
				}


			}
		}

		cout<<"The average for the month of February is "<<average<<"."<<endl;
		cout<<"The month with the lowest temp is "<<month<<" and the temperature is "<<lowest<<".";
	}

    return 0;
}

And this is the data file:

0.0   31   6 0.0   44  16 1.12  62  48
2.31  74  50 2.88  77  53 2.45  81  50 1.18  92  66  .34  98  74 .91  
1.03  68  44  .78  49  31
0.0   43  26

and this is my output:

0 31 6 0 44 16 1.12 62 48 2.31 74 50 2.88 77 53 2.45 81 50 1.18 92 66 0.34 98 74
 0.91 87 59 1.03 68 44 0.78 49 31 0 43 26 -1.07374e+008 -1.07374e+008 -1.07374e+
008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.073
74e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1
.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+00
8 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374
e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.0
7374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008
-1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+
008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.073
74e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1
.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+00
8 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374
e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.0
7374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008
-1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+
008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 The av
erage for the month of February is -9.97046e+007.
The month with the lowest temp is February and the temperature is -1.07374e+008.
Press any key to continue . . .

Recommended Answers

All 3 Replies

Your numbers ARE being read. In fact if you look closer at your output You can see, that they all are read and something more is read (some trash). Why? You have only 34 values in your data file, but you try to read 124 values ( SIZE_MONTH *SIZE_DAYS ) from the stream.

You could either modify the for loop reading the stream to regulate it to read only till EOF or add more elements to your data file

It looks like your program is working perfectly well.

That is, it's doing what you've told it to do.

Two areas of concern.
First, you don't have enough data to fill that array. You've got 34 or so data elements, and the matrix is sized for 124. So after the data is read in, the rest of your matrix is filled with (or retains its initial) garbage, as should be noted by all the numbers being the same value (that's a feature of running debug mode.)

Second, you don't account for different lengths of months. For your February average, you sum 31 days of data, then divide by 28.

I'll give you points for initially setting the value of lowest to the first data element! So many students fail to do that. Your string month should also be initialized to the first month name.

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.