RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 334 | Replies: 4 | Thread Tools  Display Modes
Reply
Join Date: Jul 2008
Posts: 21
Reputation: 2fac323 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
2fac323 2fac323 is offline Offline
Newbie Poster

Passing infile values??

  #1  
Jul 31st, 2008
This program needs to read the infile and calculate the temperatures for each function. I can only get it to output 0's. Can someone explain the steps needed to make this code work correctly? What am I missing? Should I be passing by reference? If so how??

#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.txt");
       
       
       
       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;
       }
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,861
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 1012
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Passing infile values??

  #2  
Jul 31st, 2008
Look at your function getData(). It isn't reading the data from the file -- all it is reading is two numbers called low and high. What its supposed to do is read all the numbers into the array temp. You will have to add a loop so that it can do that.
<<Hire Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Jul 2008
Posts: 21
Reputation: 2fac323 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
2fac323 2fac323 is offline Offline
Newbie Poster

Re: Passing infile values??

  #3  
Jul 31st, 2008
Ok, I have narrowed it down to me needing a while loop. But I am having trouble understanding how to use it correctly. Can someone give me an example of a while loop that reads from a one column in a txt infile and reads to the end?
Here is what I came up with-
infile example:
34 -12
30 -21
32 -18
40 -5
52 10
75 32

while (inFile)
       {
             if (row = 0; row < 13; row++)
             {
                     inFile >>high >> low;
                     }
             if (col = 0; col < 13; col++)
             {
                     inFile>>high >>low;
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,861
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 1012
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Passing infile values??

  #4  
Jul 31st, 2008
does the file contain those dashes you posted ? If not, then just do something like this:
while( inFile >> temp[i][0] >> temp[i][1])
{
    ++i;
}

you have the rows and columns of that temp array backwards. row should be 12 and columns should be 2. You want the array to look something like this:
  Col1  Col2
|------|-------|
|------|-------|
|------|-------|
|------|-------|
|------|-------|
|------|-------|
|------|-------|
|------|-------|
|------|-------|
|------|-------|
|------|-------|
|------|-------|
Last edited by Ancient Dragon : Jul 31st, 2008 at 1:22 pm.
<<Hire Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Jul 2008
Posts: 21
Reputation: 2fac323 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
2fac323 2fac323 is offline Offline
Newbie Poster

Re: Passing infile values??

  #5  
Jul 31st, 2008
Thanks alot! I am sure this will help me.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 8:16 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC