I was spending a lot of time looking around this site to practice with beginner programs. I found one posted by sniper1. And, I tried to make it work. But, now am stuck! Can someone please tell me what is wrong with my code.

Problem:
1. [SOLO] Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and the highest and lowest temperatures for the year. Your program must consist of the following functions:
a. Function getData:This function reads and stores data in the two dimensional array.
b. Function averageHigh:This function calculates and returns the average high temperature for the year.
c. Function averageHigh:This function calculates and returns the average low temperature for the year.
d. Function indexHighTemp:This function returns the index of the highest high temperature in the array.
e. Function indexLowTemp:this function returns the index of the lowest low temperature in the array.(These functions must all have the appropriate parameters.)

I also wanted the function getData to get the data from a text file which I made. Can't seem to get that to work either.
I don't even know if I am on the right track, so any advice would be beneficial. And I have been trying to post my code the correct way [ / code] but, can't even seem to get that to work. So please bear with me.

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


using namespace std;

double getData(ifstream inputF, double high, double low);
double averageHigh();
double averageLow();
double indexHighTemp();
double indexLowTemp();

int i, j;
double hsum = 0, lsum = 0;
double avgh, avgl;
double high = 0, low = 0;
int ind;
int const row = 2, col = 12, num = 12;
double temp[row][col];

int main()
{
    ifstream inp;
    ifstream inputF;
    
    if (!inp)
    {
             cout << "Unable to open input file!"<<endl;
             return 1;
             } 
    inputF.open("c:\Ch9_Ex9Data");
    getData(inp);
    averageHigh();
    averageLow();
    indexHighTemp();
    indexLowTemp();
    

  system("PAUSE");
  return 0;
}
double getData(ifstream inputF, double high, double low)
{
       inputF >> high >> low;
       }
double averageHigh()
{
       for (i = 0; i < col; i++)
       {
           hsum = (hsum + (temp[0][i]));
           }
       avgh = hsum / 12;
       cout << "Average High Temperature: "<< endl;
       cout << avgh;
       return 0;
       }
double averageLow()
{
       for (i = 0; i < col; i++)
       {
           lsum = (lsum + (temp[1][i]));
           }
       avgl = lsum / 12;
       cout << "Average Low Temperature: "<< endl;
       cout << avgl;
       return 0;
       }
double indexHighTemp()
{
       ind = 0;
       for (i = 0; i < col; i++)
       {
           if (high <= (temp[0][i]))
           {
                    high = (temp[0][i]);
                    ind = i;
                    }
           }
       cout << "Highest Temperature: "<< endl;
       cout << (temp[0][ind]);
       return 0;
       }
double indexLowTemp()
{
       ind = 0;
       for (i = 0; i < col; i++)
       {
           if (low >= (temp[1][i]))
           {
                   low = (temp[1][i]);
                   ind = i;
                   }
                   }
       cout << "Lowest Temperature: "<< endl;
       cout << (temp[1][ind]);
       return 0;
       }

The few mistakes i have observed. You need to open the input file stream before actually checking for the status i.e if it exists or not.

Secondly, you do not need to pass the file stream through a function. if it has already been declared in main then you just have to use it the same way you use your cin statements.

A 2D array contains two for loops you may have wanted something like this

for(int i=0;i<row;i++)
{
       for(int j=0;j<col;j++)
       {
           //your code here
       }
}

And lastly you need to practice good indentation. Fix the few errors I have pointed out then post your problem again

Ok I have changed a few things from the advice I was given. And, I believe I almost have it, but I cannot get the infile to read. Keeps saying it is unable to find the infile. So why cant it find the infile??

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


using namespace std;

double getData();
double averageHigh();
double averageLow();
double indexHighTemp();
double indexLowTemp();

int i, j;
double hsum = 0, lsum = 0;
double avgh, avgl;
double high = 0, low = 0;
int ind;
int const row = 2, col = 12, num = 12;
double temp[row][col];

int main()
{
    cout <<"Processing Data"<<endl;
    getData();
    averageHigh();
    averageLow();
    indexHighTemp();
    indexLowTemp();
    cout <<" "<<endl;

  system("PAUSE");
  return 0;
}
double getData()
{
       ifstream inFile;
       
       inFile.open ("c:\Ch9_Ex9Data");
       
       inFile >> high >> low;
       
       if (!inFile)
       {
             cout << "Unable to open input file!"<<endl;
             return 1;
             } 
       
       }
double averageHigh()
{
       for(int i=0;i<row;i++)
       {
               for(int j=0;j<col;j++)
               {
                       hsum = (hsum + (temp[0][i]));
                       }
           }
       avgh = hsum / 12;
       cout << "Average high temperature: ";
       cout << avgh <<endl;
       return 0;
       }
double averageLow()
{
       for(int i=0;i<row;i++)
       {
               for(int j=0;j<col;j++)
               {
                       lsum = (lsum + (temp[1][i]));
                       }
           }
       avgl = lsum / 12;
       cout << "Average low temperature: ";
       cout << avgl << endl;
       return 0;
       }
double indexHighTemp()
{
       ind = 0;
       for(int i=0;i<row;i++)
       {
               for(int j=0;j<col;j++)
               {
                       if (high <= (temp[0][i]))
                       {
                                high = (temp[0][i]);
                                ind = i;
                       }
                       }
           }
       cout << "Highest temperature: ";
       cout << (temp[0][ind]) << endl;
       return 0;
       }
double indexLowTemp()
{
       ind = 0;
       for(int i=0;i<row;i++)
       {
               for(int j=0;j<col;j++)
               {
                       if (low >= (temp[1][i]))
                       {
                               low = (temp[1][i]);
                               ind = i;
                   }
                   }
                   }                  
       cout << "Lowest temperature: ";
       cout << (temp[1][ind]) << endl;
       return 0;
       }

Try replacing

inFile.open ("c:\Ch9_Ex9Data");

with

inFile.open ("c:\\Ch9_Ex9Data");

It's interpreting the '\' as an escape character rather than a backslash, so you need two of them.

Secondly, you do not need to pass the file stream through a function. if it has already been declared in main then you just have to use it the same way you use your cin statements.

If the stream is declared in main(), how do you expect to use it in another function if you don't pass it to that function? The stream has to be passed BY REFERENCE to other functions. cin doesn't have to be passed because its declared globally.

getData() must be declared like this: see RED double getData(ifstream& inputF, double high, double low) BUT, BUT the way the op posted it last is the better way to do that -- declare the stream object inside the function that will use it.

Tried that also, but did not work. Still wont open the infile??

Tried that also, but did not work. Still wont open the infile??

You're getting "Unable to open input file!"? Make sure the file has no extension on it and try checking the file IMMEDIATELY after opening it.

double getData()
{
       ifstream inFile;
       
       inFile.open ("c:\\Ch9_Ex9Data");
        
       if (!inFile)
       {
             cout << "Unable to open input file!"<<endl;
             return 1;
        } 


       inFile >> high >> low;
}

try this: There is no reason to declare the function returning double when it only returns an integer.

int getData()
{
       ifstream inFile;
       
       inFile.open ("c:\\Ch9_Ex9Data");
            
       if (!inFile)
       {
             cout << "Unable to open input file!"<<endl;
             return 1;
             } 
       
       inFile >> high >> low;
       return 0;
       }

Ok- I have tried all of the advice given to me. But, I still can not get it to open the infile. Is there a possibility I am telling it to read the infile wrong? Here is a copy of my infile and my updated code.
infile:
34 -12
30 -21
32 -18
40 -5
52 10
75 32
85 49
92 53
88 31
65 9
42 -17
39 -19

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


using namespace std;

int getData();
double averageHigh();
double averageLow();
double indexHighTemp();
double indexLowTemp();

int i, j;
double hsum = 0, lsum = 0;
double avgh, avgl;
double high = 0, low = 0;
int ind;
int const row = 2, col = 12, num = 12;
double temp[row][col];

int main()
{
    cout <<"Processing Data"<<endl;
    getData();
    averageHigh();
    averageLow();
    indexHighTemp();
    indexLowTemp();
    cout <<" "<<endl;

  system("PAUSE");
  return 0;
}
int getData()
{
       ifstream inFile;
       
       inFile.open ("c:\\Ch9_Ex9Data");
       
       
       
       if (!inFile)
       {
             cout << "Unable to open input file!"<<endl;
             return 1;
             } 
       inFile >> high >> low;
       
       }
double averageHigh()
{
       for(int i=0;i<row;i++)
       {
               for(int j=0;j<col;j++)
               {
                       hsum = (hsum + (temp[0][i]));
                       }
           }
       avgh = hsum / 12;
       cout << "Average high temperature: ";
       cout << avgh <<endl;
       return 0;
       }
double averageLow()
{
       for(int i=0;i<row;i++)
       {
               for(int j=0;j<col;j++)
               {
                       lsum = (lsum + (temp[1][i]));
                       }
           }
       avgl = lsum / 12;
       cout << "Average low temperature: ";
       cout << avgl << endl;
       return 0;
       }
double indexHighTemp()
{
       ind = 0;
       for(int i=0;i<row;i++)
       {
               for(int j=0;j<col;j++)
               {
                       if (high <= (temp[0][i]))
                       {
                                high = (temp[0][i]);
                                ind = i;
                       }
                       }
           }
       cout << "Highest temperature: ";
       cout << (temp[0][ind]) << endl;
       return 0;
       }
double indexLowTemp()
{
       ind = 0;
       for(int i=0;i<row;i++)
       {
               for(int j=0;j<col;j++)
               {
                       if (low >= (temp[1][i]))
                       {
                               low = (temp[1][i]);
                               ind = i;
                   }
                   }
                   }                  
       cout << "Lowest temperature: ";
       cout << (temp[1][ind]) << endl;
       return 0;
       }

Either the file isn't where you think it is or you have the wrong filename. Use Windows Explorer to find how where the file is located and its exact filename, including any extension. Does the file have *.txt extension? If yes, then you need to add that to the filename in the open statement. I ran your program and it opened the file ok for me after correcting the filename.

Thanks for your help. I overlooked the easiest solution. Assuming it was a problem with my code. Now, can anyone help me to understand why I am only receiving 0's in my output for temperatures?

If the stream is declared in main(), how do you expect to use it in another function if you don't pass it to that function? The stream has to be passed BY REFERENCE to other functions. cin doesn't have to be passed because its declared globally.

getData() must be declared like this: see RED double getData(ifstream& inputF, double high, double low) BUT, BUT the way the op posted it last is the better way to do that -- declare the stream object inside the function that will use it.

Thanks for the correction. I forgot that you have to declare it in the function in which you are using it and that is why there is no need to pass it by value or reference.

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.