Reading info from a file into arrays

closed account (2EURX9L8)
I am not sure what I am doing wrong but I am only getting the first number in the txt file.


txt file is like this

4708708 Alabama
698473 Alaska
6595778 Arizona
2889450 Arkansas
36961664 California
5024748 Colorado
3518288 Connecticut
885122 Delaware


the code I have so far, havent set up the output formating

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//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>
using namespace std;


int main()

{

	const int 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;
}
closed account (2EURX9L8)
is it 99 values with two rows of 50 for a parallel array? Do I need to do some kind of getline?
1
2
3
4
5
	while(count<SIZE && fin>>population[count])
	{
		fin>>population[count]>>stateName[count];
		count++;
	}


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++] ;


Btw, 99 values for two rows of 50? SIZE is 49?
Last edited on
closed account (2EURX9L8)
I mean 50 lines with two rows of info, population and the state name
closed account (2EURX9L8)
How do I use getline when using arrays while reading from a file?

this is reading everything right but the states with a white space, like District of Columbia, is only displaying District.
1
2
3
4
5
6
int count=0;
	while(count<SIZE && getline(fin,stateName[count]))
	{
		fin>>population[count]>>stateName[count];
		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++] ) ;
     }

closed account (2EURX9L8)
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.

the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
Last edited on
1
2
3
4
5
6
7
for(count=0; count<51; count++){

	if(population[count]>maxPop)
		maxPop=population[count];

	maxName=stateName[count];
}


Fixed your indentation. That might make it a little clearer what's going on.
closed account (2EURX9L8)
Got it, thanks again.
Topic archived. No new replies allowed.