//This program will read the population and name of each of the 50 states including the District of Columbia,
//and stores them in parallel arrays. After the data is stored this program will
//print and compute the name and population of each state and the district of columbia,
#include <iostream>
#include <fstream>
#include<iomanip>
#include<string>
usingnamespace std;
int main()
{
constint SIZE=49;
int population[SIZE];
string stateName[SIZE];
//code to open the file statePopulation.txt and display an error message if it is not found
ifstream fin("statePopulation.txt");
if(!fin){
cout<<"Error opening file. Shutting down....\n";
return 1;}
int count=0;
while(count<SIZE && fin>>population[count])
{
fin>>population[count]>>stateName[count];
count++;
}
cout<<"State"<<"Population"<<endl;
for(int index=0; index<count; index++){
cout<<population[index]<<" "<<stateName[index]<<endl;
}
return 0;
}
If the file format is as you specify your first number is read into population.
When you enter the body of the loop, then, you try to read the state after the first number into population. However, it's not a number so it puts fin into an error state, and that is the end of your input.
1 2
while ( count < SIZE && fin )
fin >> population[count] >> stateName[count++] ;
That doesn't read everything right. You're reading two state names for every population number.
1 2 3 4 5 6 7
int count=0;
while ( count < SIZE && fin >> population[count] )
{
while ( cin.peek() == ' ' ) // discard the whitespace separating state name and
cin.get() ; // population.
getline( cin, stateName[count++] ) ;
}
one last thing, the largest state population name is the same as my smallest. I bet I am doing the accumulators all wrong, thank you though for all your help by the way.
int maxPop=population[0];
string maxName;
//for loop to read and calculate the largest population
for(count=0; count<51; count++){
if(population[count]>maxPop)
maxPop=population[count];
maxName=stateName[count];
}
cout<<"\nThe largest population is: "<<maxPop<<" in the state of "<<maxName<<endl;
int smallestPop=population[0];
string smallestName;
//for loop to get the smallest population and set the vaule to smallestPop variable and its name to smallestName
for(count=0;count<51; count++){
if(population[count]<smallestPop)
smallestPop=population[count];
smallestName=stateName[count];
}
cout<<"\nThe smallest population is: "<<smallestPop<<" in the state of "<<smallestName<<endl;
The full file I am reading from is this:
4708708 Alabama
698473 Alaska
6595778 Arizona
2889450 Arkansas
36961664 California
5024748 Colorado
3518288 Connecticut
885122 Delaware
599657 District of Columbia
18537969 Florida
9829211 Georgia
1295178 Hawaii
1545801 Idaho
12910409 Illinois
6423113 Indiana
3007856 Iowa
2818747 Kansas
4314113 Kentucky
4492076 Louisiana
1318301 Maine
5699478 Maryland
6593587 Massachusetts
9969727 Michigan
5266214 Minnesota
2951996 Mississippi
5987580 Missouri
974989 Montana
1796619 Nebraska
2643085 Nevada
1324575 New Hampshire
8707739 New Jersey
2009671 New Mexico
19541453 New York
9380884 North Carolina
646844 North Dakota
11542645 Ohio
3687050 Oklahoma
3825657 Oregon
12604767 Pennsylvania
1053209 Rhode Island
4561242 South Carolina
812383 South Dakota
6296254 Tennessee
24782302 Texas
2784572 Utah
621760 Vermont
7882590 Virginia
6664195 Washington
1819777 West Virginia
5654774 Wisconsin
544270 Wyoming