heres my code:
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;
struct WeatherInfo
{
string City; //Name of the city
double Rainfall; // Total rain fall of all citys
double High; //Highest temperature of all citys
double Low; //Lowest temperature of all citys
double Average; //Average temperature of all citys
};
ifstream infile;;
void fillCities(WeatherInfo cities[],int &numElems);
void PrintCities(WeatherInfo cities[],int numElems);
void OrderCities(WeatherInfo cities[],int size);
int AvgRainFall(WeatherInfo cities[],int NumCities);
int FindAvgTemp(WeatherInfo cities[],string value ,int NumCities);
int main()
{
infile.open("prog1.txt");
string value;
const int SIZE = 30;
int Size;
int result;
//string value;
WeatherInfo cities[SIZE];
fillCities (cities,Size);
PrintCities(cities,SIZE);
OrderCities(cities,SIZE);
PrintCities(cities,SIZE);
AvgRainFall(cities,SIZE);
result = FindAvgTemp(cities,value,SIZE);
infile.close();
return 0;
}
void fillCities(WeatherInfo cities[],int &numElems)
{
string tname;
getline(infile,tname);
int i = 0;
while(!infile.eof())
{
cities[i].City = tname;
infile>>cities[i].Rainfall;
infile.ignore();
infile>>cities[i].High;
infile>> cities[i].Low;
cities [i].Average = (cities[i].High + cities[i].Low)/2;
infile.ignore();
i++;
getline(infile,tname);
}
}
void PrintCities(WeatherInfo cities[],int numElems)
{
int index;
cout<< left;
cout<<"---------------------------------------------------------"<<endl;
cout<<setw(18)<<"City"<<setw(12)<<"Rain Fall"
<<setw(10)<<"Highs"<<setw(10)<<"Lows"
<<setw(10)<<"Avg"<<endl;
cout<<"---------------------------------------------------------"<<endl;
for (index = 0;index < numElems; index ++)
{
cout<< setw(20)<<cities[index].City;
cout<< setw(10)<<cities[index].Rainfall;
cout<< setw(10)<<cities[index].High;
cout<< setw(10)<<cities[index].Low;
cout<< setw(10)<<cities[index].Average<<endl;
}
}
void OrderCities(WeatherInfo cities[],int numElems)
{
bool swap;
WeatherInfo temp;
do
{
swap = false;
for (int i = 0;i < (numElems - 1); i++)
{
if (cities[i].City > cities[i + 1].City )
{
temp = cities [i];
cities[i]= cities[i + 1];
cities[i + 1] = temp;
swap = true;
}
}
}while (swap);
}
int FindAvgTemp(WeatherInfo cities[],string value,int NumCities)
{
cout<< "Enter a city ";
cin>> cities[NumCities].City;
int i;
for(i=0;i<NumCities;i++)
{
int first = 0;
int last = NumCities - 1;
int middle,
position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last)/2;
if (cities[middle].City == value)
{
found = true;
position = middle;
}
else if (cities[middle].City > value)
last = middle - 1;
else
first = middle + 1;
}
cout<<setw(5)<<cities[NumCities].City <<" has a Average Temperture "<<setw(10)<<cities[NumCities].Average<<endl;
return position;
}
}
int AvgRainFall(WeatherInfo cities[],int NumCities)
{
int index;
double Sum = 0;
double Avg;
for(index = 0;index < NumCities; index++)
{
Sum += cities[index].Rainfall;
}
Avg = Sum / NumCities;
cout<<"---------------------------------------------------------"<<endl;
cout<<"Average Rain fall is " <<Avg<<endl;
cout<<"---------------------------------------------------------"<<endl;
return 0;
}
heres actual the result
-------------------------------------------------------------
City Rain Highs Lows Avg
---- ---- ----- ---- ---
Dallas 3.5 85 5 45
Boston 4.5 56 8 32
Bowling Green 3.1 89 6 47.5
Houston 7.5 54 11 32.5
Orlando 3.5 78 12 45
Baltimore 4.5 89 14 51.5
New York 5.5 62 7 34.5
New Orleans 2.5 75 3 39
Boca raton 5.5 68 27 47.5
Merida 6.5 105 8 56.5
Morehead City 1.5 97 15 56
Norfolk 2.5 64 0 32
Elizabeth City 4.5 78 9 43.5
Key West 3.5 97 5 51
Daytona 5.9 92 7 49.5
Dayton 7.2 89 2 45.5
San Jose 2.4 101 4 52.5
Chicago 1.8 95 6 50.5
Austin 5.2 88 2 45
-------------------------------------------------------------
City Rain Highs Lows Avg
---- ---- ----- ---- ---
Austin 5.2 88 2 45
Baltimore 4.5 89 14 51.5
Boca raton 5.5 68 27 47.5
Boston 4.5 56 8 32
Bowling Green 3.1 89 6 47.5
Chicago 1.8 95 6 50.5
Dallas 3.5 85 5 45
Dayton 7.2 89 2 45.5
Daytona 5.9 92 7 49.5
Elizabeth City 4.5 78 9 43.5
Houston 7.5 54 11 32.5
Key West 3.5 97 5 51
Merida 6.5 105 8 56.5
Morehead City 1.5 97 15 56
New Orleans 2.5 75 3 39
New York 5.5 62 7 34.5
Norfolk 2.5 64 0 32
Orlando 3.5 78 12 45
San Jose 2.4 101 4 52.5
-------------------------------------------------------------
The average rain fall is 4.26842.
-------------------------------------------------------------
Enter the city to be found: New York
New York's average temperature is 34.5
my problem is in the getline statemen and the binary search
with the getline statement my output has extra stuff i was wondering where to put the .ignore and with the binary search i cant get the display to produce the city that was entered and the cities average temperature can anybody help me