When I run the programme, it shows some completely random number as the lowest temperature. So, it seems like there is something wrong with the function-VectorList::GetMinF(). But, I could not figure out what exactly is the problem. So, any more help to solve this problem will be greatly appreciated.
Here is the revised code for the programme:
Code:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class PollenAirQuality
{
public:
PollenAirQuality() {PollenCount= AirIndex= 0; }
void SetPollenCount(double Count) { PollenCount = Count; }
double GetPollenCount() { return PollenCount;}
void SetAirIndex(double Index) { AirIndex = Index; }
double GetAirIndex() {return AirIndex; }
protected:
double PollenCount;
double AirIndex;
};
class WeatherStation : public PollenAirQuality
{
public:
WeatherStation();
void SetDesignation(string ID) { StationDesignation = ID; }
void SetAgent(string Agent) { StationAgent = Agent; }
void SetTemperature(double Degree) { Temperature = Degree; }
double Convert(double DegreeF);
string GetDesignation() { return StationDesignation; }
string GetAgent() { return StationAgent; }
double GetTemperature() { return Temperature; }
protected:
string StationDesignation;
string StationAgent;
double Temperature;
};
WeatherStation::WeatherStation()
{
StationDesignation = "";
StationAgent = "";
Temperature = 0;
}
double WeatherStation::Convert(double degreeF)
{
double degreeC;
// convert Fahrenheit to Celcius
degreeC = ((degreeF - 32) * 5) / 9;
return degreeC;
}
class VectorList
{
vector<WeatherStation> List;
vector<WeatherStation>::iterator ILister;
string FileName;
public:
VectorList() { FileName = "Temp"; }
VectorList(string FName) { FileName = FName; }
void AddStations();
void PostStationInfo();
void DailyReport();
void HighLowReport();
void PollenCount();
void AirQualityIndex();
double GetMaxF();
double GetMinF();
double GetAvgF();
void SetFileName(string FName) { FileName = FName; }
void WriteToFile();
void ReadFromFile();
};
void VectorList::AddStations()
{
WeatherStation Station;
string Designation, Agent;
cout << "Enter Station Information Below, Stop To Quit\n";
for (;;)
{
cout << "\n-------------------------------\n";
cout << "Enter Weather Station Designation: ";
getline (cin, Designation);
if (Designation == "Stop")
break;
Station.SetDesignation( Designation );
cout << "\nEnter Contact Person: ";
getline (cin, Agent);
if (Agent == "Stop")
break;
Station.SetAgent( Agent );
cout << "\n-------------------------------\n";
List.push_back(Station);
}
}
void VectorList::PostStationInfo()
{
double Degree, Count, Index;
cout << "Enter reported temperatures: (in Fahrenheit) \n\n";
for (ILister = List.begin(); ILister < List.end(); ILister++)
{
cout << ILister->GetDesignation() << ":\n";
cout << ILister->GetAgent();
cout << "\nEnter Temperature:\t";
cin >> Degree;
ILister->SetTemperature( Degree );
cout << "\n\tPollen Count:\t";
cin >> Count;
ILister->SetPollenCount( Count );
cout << "\n\tAir Quality Index:\t";
cin >> Index;
ILister->SetAirIndex( Index );
cin.ignore(100, '\n');
}
}
void VectorList::DailyReport()
{
cout << " NGS Daily Temperature Report \n";
cout << "===========================================================\n";
cout << "\t\t Fahrenheit(°F) \t Celsius (°C)";
for (ILister = List.begin(); ILister < List.end(); ILister++)
{
cout << "\n-----------------------------------------------------------\n";
cout << (*ILister).GetDesignation() << "\t\t ";
cout << (*ILister).GetTemperature() << "\t\t\t"
<< (*ILister).Convert( (*ILister).GetTemperature() );
}
cout << "\n-----------------------------------------------------------";
cout << "\nMean Temperature: \t " << GetAvgF() // The first problem is here
<< "\t\t\t" << (*ILister).Convert(GetAvgF() );
cout << "\n===========================================================\n\n";
}
void VectorList::HighLowReport()
{
cout << " NGS High-Low Report ";
cout << "\n==========================================================";
cout << "\n\t\t Fahrenheit(°F) \t Celsius (°C)";
cout << "\n----------------------------------------------------------";
cout << "\nLowest Temperature:\t " << GetMinF() //Second problem
<< "\t\t\t" << (*ILister).Convert( GetMinF() );
cout << "\n----------------------------------------------------------";
cout << "\nHighest Temperature:\t " << GetMaxF() //Third problem
<< "\t\t\t" << (*ILister).Convert( GetMaxF() );
cout << "\n----------------------------------------------------------";
cout << "\n==========================================================\n";
}
void VectorList::PollenCount()
{
cout << " NGS Daily Pollen Report\n";
cout << "==================================\n";
for (ILister = List.begin(); ILister < List.end(); ILister++)
{
cout << ILister->GetDesignation() << ":\t\t" << ILister->GetPollenCount() << "\n";
}
cout << "==================================\n\n";
}
void VectorList:: AirQualityIndex()
{
cout << " NGS Air Quality Index Report\n";
cout << "==================================\n";
for (ILister = List.begin(); ILister < List.end(); ILister++)
{
cout << ILister->GetDesignation() << ":\t\t" << ILister->GetAirIndex() << "\n";
}
cout << "==================================\n\n";
}
double VectorList::GetMaxF()
{
double maxF;
maxF = ILister->GetTemperature();
for (ILister = List.begin(); ILister < List.end(); ILister++)
{
// find the highest temperture in Fahrenheit
if ( (*ILister).GetTemperature() > maxF)
maxF = (*ILister).GetTemperature();
}
return maxF;
}
double VectorList::GetMinF()
{
double minF;
minF = ILister->GetTemperature();
for (ILister = List.begin(); ILister < List.end(); ILister++)
{
// find the lowest temperture in Fahrenheit
if ( (*ILister).GetTemperature() < minF)
minF = (*ILister).GetTemperature();
}
return minF;
}
double VectorList::GetAvgF()
{
double Total = 0, Mean;
for (ILister = List.begin(); ILister < List.end(); ILister++)
Total += ILister->GetTemperature();
Mean = Total / List.size();
return Mean;
}
void VectorList::WriteToFile()
{
fstream OutFile(FileName.c_str(), ios::out);
for ( ILister = List.begin(); ILister < List.end(); ILister++)
{
OutFile << ILister->GetDesignation() << endl;
OutFile << ILister->GetAgent() << endl;
OutFile << ILister->GetTemperature() << endl;
OutFile << ILister->GetPollenCount() << endl;
OutFile << ILister->GetAirIndex() << endl;
}
OutFile.close();
}
void VectorList::ReadFromFile()
{
fstream InFile(FileName.c_str(), ios::in);
string Designation, Agent;
double Temperature, Count, Index;
WeatherStation Temp;
while(true)
{
getline(InFile, Designation);
getline(InFile, Agent);
InFile >> Temperature;
InFile >> Count;
InFile >> Index;
InFile.ignore(100, '\n');
if(InFile.eof())
break;
Temp.SetDesignation( Designation );
Temp.SetAgent( Agent );
Temp.SetTemperature( Temperature );
Temp.SetPollenCount( Count );
Temp.SetAirIndex( Index );
List.push_back(Temp);
}
InFile.close();
}
void Menu();
int main()
{
int I = 0;
VectorList Station;
string Command;
while(true)
{
Menu();
cout << "Enter Command: ";
getline (cin, Command);
cout << "\n\n";
if (Command == "Add Stations")
{
Station.AddStations();
I = 1;
}
if (Command == "Post Station Info" && I == 1)
{
Station.PostStationInfo();
I = 2;
}
if (Command == "Quit")
break;
else
{
if ( I == 1 )
cout << "\nPlease Post Station Info\n";
if (Command == "Daily Report" && I == 2)
Station.DailyReport();
if (Command == "High-Low Report" && I == 2)
Station.HighLowReport();
if (Command == "Daily Pollen Count" && I == 2)
Station.PollenCount();
if (Command == "Daily Air Quality Index" && I == 2)
Station.AirQualityIndex();
if (Command == "Change File Name")
{
string FName;
cout << "Enter File Name: ";
getline(cin, FName);
Station.SetFileName(FName);
cout << "File Name is Changed";
}
if (Command == "Save To File")
{
Station.WriteToFile();
cout << "File is saved\n";
}
if (Command == "Read From File")
{
Station.ReadFromFile();
I = 2;
cout << "Opened Saved File\n";
}
}
}
}
void Menu()
{
cout << "\nChoices:";
cout << "\n---------------------------";
cout << "\n Add Stations";
cout << "\n Post Station Info";
cout << "\n Daily Report";
cout << "\n High-Low Report";
cout << "\n Daily Pollen Count";
cout << "\n Daily Air Quality Index";
cout << "\n Change File Name";
cout << "\n Save To File";
cout << "\n Read From File";
cout << "\n Quit";
cout << "\n---------------------------\n";
}
Just for information, when my actual lowest temperature was 48 Fahrenheit Celsius, the programmed showed the lowest temperature was -1.677.
Originally Posted by
jimblumberg
If you are still having problems with the GetMinF() function displaying the maximum instead of the minimum you may want to change the greater than operator to the less than operator in you if statement
Code:
if ( (*ILister).GetTemperature() > minF)
Is your maximum function working correctly? They should be almost the same.
Jim
Jim, I changed the greater than operator to the less than operator; however, now, it shows some random number, which is not even my input temperature, as the lowest temperature.