C++ problem data sorting: I got my own code below but something went wrongthe de
ID: 3861151 • Letter: C
Question
C++ problem data sorting:
I got my own code below but something went wrongthe delimiter symbol just for me to test
void SortByNum()
{
string list[50];
string STATES[15];
string CAP[15];
long POP[20];
for(int i=0;i<=12;i++)
{
getline(fIn,list[i]);
removeStuff(list[i], ','); //my function to remove the commas in text
stringstream ssin(list[i]);
ssin>>STATES[i]>>CAP[i]>>POP[i];
cout<
}
fIn.close();
}
the data.txt is:
Idaho Boise 1,297,274
Washington Olympia 5,908,684
Vermont Montpelier 609,890
Hawaii Honolulu 1,216,642
Arizona Phonenix 5,140,683
Montana Helena 905,316
Virginia Richmond 7,100,702
North Dakota Bismarck 643,756
Colorado Denver 4,311,882
Mississippi Jackson 2,852,927
Delaware Dover 785,068
Georgia Atlanta 8,206,975
South Carolina Columbia 4,025,061
however my output is:
Idaho | Boise | 1297274
Washington | Olympia | 5908684
Vermont | Montpelier | 609890
Hawaii | Honolulu | 1216642
Arizona | Phonenix | 5140683
Montana | Helena | 905316
Virginia | Richmond | 7100702
North | Dakota | 0
Colorado | Denver | 4311882
Mississippi | Jackson | 2852927
Delaware | Dover | 785068
Georgia | Atlanta | 8206975
South | Carolina | 0
which line 8 is missing the population cuz the state was North Dakota which has two word; pleas help me fix it and please after reading the data sort them by the population in ascending order THANKS!
Explanation / Answer
string token;
int i =0;
string temp;
while(getline(ssin, token, ' ')) {
if(i==0){
STATES[0] = token;
}
if(i==1){
CAP[0] = token;
//i++;
}
if(i==2){
temp = token;
//i++;
}
if(i==3){
POP[0] = stoi(token);
STATES[0] += " "+ CAP[0];
CAP[0] = temp;
//i++;
}
i++;
}
if(i==3) POP[0] = stoi(token);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.