The function is designed to
1.post the temperatures that come in from the various weather stations
2.display the temperatures in both fahrenheit and celsius for each weather station
3.calculate and display the highest temperature as well as the lowest among the five stations.
#include <std_lib_facilities.h>
usingnamespace std;
struct WeatherStation {
string StationDesignation;
double Temperature;
};
void Post(vector<WeatherStation>&);
void HighLow(vector<WeatherStation>&);
void Daily(vector<WeatherStation>&);
void Menu();
int main()
{
vector<WeatherStation> List;
string Command="";
while(Command!="Quit")
{
Menu();
cout<<"Command: ";
getline(cin,Command);
if(Command=="Post")
Post(List);
elseif(Command=="HighLow")
HighLow(List);
elseif(Command=="Daily")
Daily(List);
elseif(Command!="Quit"&&Command!="Post"&&Command!="HighLow"&&Command!="Daily")
cout<<"Please type one of the four words--'Post', 'HighLow', 'Daily', or 'Quit'.\n\n";
}
system("pause");
}
void Post(vector<WeatherStation>&List)
{
WeatherStation Loc;
Loc.StationDesignation="";
Loc.Temperature=0;
Loc.StationDesignation="Tciitcgatc";
List.push_back(Loc);
Loc.StationDesignation="Redwood Haven";
List.push_back(Loc);
Loc.StationDesignation="Barrier Mts.";
List.push_back(Loc);
Loc.StationDesignation="Nina`s Folly";
List.push_back(Loc);
Loc.StationDesignation="Sooly`s Hill";
List.push_back(Loc);
Loc.StationDesignation="Twin Cones Park";
List.push_back(Loc);
cout << "\nEnter Temperatures Below..." << endl;
int i;
double Temp;
for(i=0;i<List.size();++i)
{
cout<<"\nTemperature Station: " << List[i].StationDesignation << endl;
cout<<"Temperature: ";
cin>>Temp;
List[i].Temperature=Temp;
}
cout<<endl;
cin.ignore(100, '\n');
}
void HighLow(vector<WeatherStation>&List)
{
int i;
double low=List[0].Temperature;
for(i=1;i<List.size();++i)
low=List[i].Temperature>low?low:List[i].Temperature;
double high=List[0].Temperature;
for(i=1;i<List.size();++i)
high=List[i].Temperature<high?high:List[i].Temperature;
cout<<"\n\n===============================Temperature Report=============================="<<endl
<<"\n\t\t\tFahrenheit\tCelsius"<<endl
<<"-------------------------------------------------------------------------------"
<<setprecision(4)
<<"\nLowest Temperature =\t\t"<<low<<"\t"<<(low-32)*5/9<<endl
<<"-------------------------------------------------------------------------------"
<<"\nHighest Temperature =\t\t"<<high<<"\t"<<(high-32)*5/9<<endl<<endl
<<"-------------------------------------------------------------------------------\n\n";
}
void Daily(vector<WeatherStation>&List)
{
double sum=0, mean=0;
int i;
for (i=0;i<List.size();++i)
sum+=List[i].Temperature;
mean=sum/List.size();
cout<<"\n\n===============================Temperature Report=============================="
<<"\n\n\t\t\tFahrenheit\tCelsius"<<endl
<<"-------------------------------------------------------------------------------"
<<setprecision(4)
<<"\nMean Temperature =\t"<<mean<<"\t\t"<<(mean-32)*5/9<<endl
<<"-------------------------------------------------------------------------------\n"
<<"Raw Data:\n\n";
for (i=0;i<List.size();++i)
cout<<List[i].StationDesignation<<"\t\t"<<List[i].Temperature<<"\t\t"<<(List[i].Temperature-32)*5/9<<endl;
cout<<"-------------------------------------------------------------------------------\n\n\n";
}
void Menu()
{
cout<<"Choices........................................................................" << endl
<<"-------------------------------------------------------------------------------"<<endl
<<"\tPost......allows the user to post temperatures" << endl
<<"\tHighLow...displays the report of high and low temperatures" << endl
<<"\tDaily.....display the daily report" << endl
<<"\tQuit......exits the program"<<endl
<<"-------------------------------------------------------------------------------\n";
}
But if I "Post" the data twice, the first time is normal--there will shows the 6 stations. But for the second time, it shows the 6 stations twice, which makes it 12. Anyone has any idea how can I fix it?
#include <cstdlib>
#include <iostream> //my guess to which libraries you are using
#include <vector>
#include <string>
usingnamespace std;//maybe move past this
struct WeatherStation {
string StationDesignation;
double Temperature;
};
void Post(vector<WeatherStation>&);
void HighLow(vector<WeatherStation>&);
void Daily(vector<WeatherStation>&);
void Menu();
int main()
{
vector<WeatherStation> List;
string Command="";
while(Command!="Quit")
{
Menu();
cout<<"Command: ";
getline(cin,Command);
if(Command=="Post")//maybe a switch statment here?
Post(List);
elseif(Command=="HighLow")
HighLow(List);
elseif(Command=="Daily")
Daily(List);
elseif(Command!="Quit"&&Command!="Post"&&Command!="HighLow"&&Command!="Daily")
cout<<"Please type one of the four words--'Post', 'HighLow', 'Daily', or 'Quit'.\n\n";
List.erase(List.begin(), List.end()); //should fix your problem
}
//system("pause"); this is bad
return 0; //you forgot to return a value
}
void Post(vector<WeatherStation>&List)
{
WeatherStation Loc;
Loc.StationDesignation="";
Loc.Temperature=0;
Loc.StationDesignation="Tciitcgatc";
List.push_back(Loc);
Loc.StationDesignation="Redwood Haven";
List.push_back(Loc);
Loc.StationDesignation="Barrier Mts.";
List.push_back(Loc);
Loc.StationDesignation="Nina`s Folly";
List.push_back(Loc);
Loc.StationDesignation="Sooly`s Hill";
List.push_back(Loc);
Loc.StationDesignation="Twin Cones Park";
List.push_back(Loc);
cout << "\nEnter Temperatures Below..." << endl;
int i;
double Temp;
for(i=0;i<List.size();++i)
{
cout<<"\nTemperature Station: " << List[i].StationDesignation << endl;
cout<<"Temperature: ";
cin>>Temp;
List[i].Temperature=Temp;
}
cout<<endl;
cin.ignore(100, '\n');
}
void HighLow(vector<WeatherStation>&List)
{
double low=List[0].Temperature;
for(int i=0;i<List.size();++i)//forgot to set i to 0 and declare in for scope
low=List[i].Temperature>low?low:List[i].Temperature;
double high=List[0].Temperature;
for(int i=0;i<List.size();++i)
high=List[i].Temperature<high?high:List[i].Temperature;
cout<<"\n\n===============================Temperature Report=============================="<<endl
<<"\n\t\t\tFahrenheit\tCelsius"<<endl
<<"-------------------------------------------------------------------------------"
<<"\nLowest Temperature =\t\t"<<low<<"\t"<<(low-32)*5/9<<endl
<<"-------------------------------------------------------------------------------"
<<"\nHighest Temperature =\t\t"<<high<<"\t"<<(high-32)*5/9<<endl<<endl
<<"-------------------------------------------------------------------------------\n\n";
}
void Daily(vector<WeatherStation>&List)
{
double sum=0, mean=0;
for (int i=0;i<List.size();++i)//declare i in the for loop scope
sum+=List[i].Temperature;
mean=sum/List.size();
cout<<"\n\n===============================Temperature Report=============================="
<<"\n\n\t\t\tFahrenheit\tCelsius"<<endl
<<"-------------------------------------------------------------------------------"
<<"\nMean Temperature =\t"<<mean<<"\t\t"<<(mean-32)*5/9<<endl
<<"-------------------------------------------------------------------------------\n"
<<"Raw Data:\n\n";
for (i=0;i<List.size();++i)
cout<<List[i].StationDesignation<<"\t\t"<<List[i].Temperature<<"\t\t"<<(List[i].Temperature-32)*5/9<<endl;
cout<<"-------------------------------------------------------------------------------\n\n\n";
}
void Menu()
{
cout<<"Choices........................................................................" << endl
<<"-------------------------------------------------------------------------------"<<endl
<<"\tPost......allows the user to post temperatures" << endl
<<"\tHighLow...displays the report of high and low temperatures" << endl
<<"\tDaily.....display the daily report" << endl
<<"\tQuit......exits the program"<<endl
<<"-------------------------------------------------------------------------------\n";
}
you also have the option so do List.resize(6) and then instead of push_back() you can just do List[iterator] = what_ever
First off, your code gets a bit messy at times. For instance, the line: elseif(Command!="Quit"&&Command!="Post"&&Command!="HighLow"&&Command!="Daily") could be replaced with just else
for(i=0;i<List.size();++i) could be changed to for(i=0;i<6;++i) and it would solve the problem.
The last thing is that I had to capitalize my commands. You should use the tolower() function to allow them user to put commands in whether they have capitals or not.
Thank you for your help,
but instead of system("pause"), what else should I use?
and I am confused that how can I use the tolower() function?
I tried to replaced elseif(Command!="Quit"&&Command!="Post"&&Command!="HighLow"&&Command!="Daily") with just else, but it shows the "Please type one of the four words--'Post', 'HighLow', 'Daily', or 'Quit'." after I input "Quit". Any other ways to solve that?
But if I "Post" the data twice, the first time is normal--there will shows the 6 stations. But for the second time, it shows the 6 stations twice, which makes it 12. Anyone has any idea how can I fix it?
In your Post function, you add 6 weather stations to the list every time you call it, so this isn't terribly surprising. The solution is to move the list building out of Post, because it doesn't belong there.