Help with a function

This is a discussion on Help with a function within the C++ Programming forums, part of the General Programming Boards category; I trying to find the highest temperature for a city then output that temperature and the city that goes with ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    11

    Help with a function

    I trying to find the highest temperature for a city then output that temperature and the city that goes with it. I'm having a lot of trouble doing that. Can you all help me?

    HIGH_city is char[25].

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    #include <cstdlib>
    
    using namespace std;
    
    class DATA {
    private:
        char city[20][25]; 
        int temps[20];
        int HIGH; 
        int LOW; 
        float AVE; 
        char HIGH_city[25]; 
        char LOW_city[25];
        int size;
    public: 
        DATA();
        void FileRead(char fname[25]); 
        void FindMax(); 
        void FindMin(); 
        void FindAve(); 
        void ShowData();
        void Print();
    };
    
    DATA::DATA()
    {
        
    }
    
    void DATA::FileRead(char fname[25])
    {
        ifstream infile(fname);
        int i = 0;
        char temp[32];
        
        while(!infile.eof())
        {
            infile.getline(temp, 30, ' ');
            strcpy(city[i],temp);
            
            infile.getline(temp, 30);
            temps[i] = atoi(temp);
            
            i++;
            size++;
        }
        infile.close();
       
    }
    
    void DATA::FindMax()
    {
         int *ptr;
         for (int i = 0; i < size; ++i)
         {
              if(temps[i] > HIGH)
                    HIGH = temps[i];//finds highest temperature
                    HIGH = *ptr;//points to what "i" is eg."temps[7]" or something
                    HIGH_city = city[HIGH];//assigns it to the pointer
                   
          }
    }
    
    void DATA::FindMin()
    {
         LOW = temps[0];
         
         for (int i = 0; i < size; i++)
         {
              if(temps[i] < LOW)
                    LOW = temps[i];
                    //LOW_city = city[i];  
         }
    }
    
    void DATA::FindAve()
    {
         static int sum;
    
         for (int i = 0; i < size; i++)
         {
              sum += temps[i];
              AVE = sum / size;
         }
    }
    
    void DATA::ShowData()
    {
         cout<<"Average: "<<AVE<<"\n";
         cout<<"Highest Temp: "<<HIGH_city<<HIGH<<"\n";
         cout<<"Lowest Temp: "<<LOW<<"\n";
         //cout<<city[6]<<"     klkl"<<endl;
    }
    
    void DATA::Print()
    {
         for(int i = 0;i < size;i++)
        {
           cout<<city[i]<<endl;
           cout<<temps[i]<<""<<endl;
           cout<<endl;
        } 
     
    }
    
    int main()
    {
            DATA *myData = new DATA();                
            myData->FileRead("a8.txt");
            myData->Print();
            
            myData->FindAve();
            myData->FindMax();
            myData->FindMin();
          
            myData->ShowData();
            
    return 0;
    }
    Last edited by Sherina; 12-04-2009 at 08:53 PM.

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by Sherina View Post
    I'm having a lot of trouble doing that.
    What are those "troubles"?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    You have a lot of variables in that class, most of which could be placed in a structure. You could then keep a vector or array of those structures. Would simplify your code a lot.
    Any particular reason you use all CAPS for the class name and some, but not all, of its members? All CAPS to me usually indicate constants or enums, not classes.

    Also seems to me your class design is a bit off. Perhaps a city class that contained the information you need to track complete with accessors and mutators would be better?

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    Quote Originally Posted by Bubba View Post
    You have a lot of variables in that class, most of which could be placed in a structure. You could then keep a vector or array of those structures. Would simplify your code a lot.
    Any particular reason you use all CAPS for the class name and some, but not all, of its members? All CAPS to me usually indicate constants or enums, not classes.

    Also seems to me your class design is a bit off. Perhaps a city class that contained the information you need to track complete with accessors and mutators would be better?
    Well this was an assignment for school and this is how my teacher wanted us to format our code.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    You're not going to get much help if you don't answer others' questions.
    What are those "troubles"?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21