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;
}