C++ problem read data from file sort them by name: I try to sort the data by nam
ID: 3860703 • Letter: C
Question
C++ problem read data from file sort them by name:
I try to sort the data by name and here is my code and the data, please help me fix it
void SortByName()
{
char line[MAX_LINE];
string stringVec;
OpenInputFile();
int i = 0;
SkipHeader();
while(fIn.getline(line,MAX_LINE) && i <= MAX_STATES)
{
cout<<"line #"<<setw(2)<<left<<i<<" "<<line<<endl;
line[i] = stringVec[i];
i++;
}
sort(stringVec.begin(),stringVec.end());
for (i=0;i<MAX_LINE;i++)
cout << stringVec << endl;
fIn.close();
}
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
Illinois Springfield 12,439,042
Nebraska Lincoln 1,715,369
Arkansas Little Rock 2,679,733
New Hampshire Concord 1,238,415
Ohio Columbus 11,374,540
Kansas Topeka 2,693,824
Louisiana Baton Rouge 4,480,271
Michigan Lansing 9,955,829
Florida Talahasse 16,028,890
Connecticut Hartford 3,409,535
Iowa Des Moines 2,931,923
West Virginia Charleston 1,813,077
Missouri Jefferson 5,606,260
Wyoming Cheyenne 495,304
California Sacramento 33,930,798
Alabama Montgomery 4,461,130
New Jersey Trenton 8,424,354
South Dakota Pierre 756,874
Maryland Annapolis 5,307,886
Indiana Indianapolis 6,090,782
Oklahoma Oklahoma City 3,458,819
North Carolina Raleigh 8,067,673
New York Albany 19,004,973
Rhode Island Providence 1,049,662
Massachusetts Boston 6,355,568
Texas Austin 20,903,994
Kentucky Frankfort 4,049,431
Alaska Juneau 628,933
Utah Salt Lake City 2,236,714
Nevada Carson City 2,002,032
Maine Augusta 1,277,731
Tennessee Nashville 5,700,037
New Mexico Santa Fe 1,823,821
Oregon Salem 3,428,543
Minnesota St. Paul 4,925,670
Wisconsin Madison 5,371,210
Pennsylvania Harrisburg 12,300,670
Explanation / Answer
I've used vectors to store each line. Vector will take Data struct as input which is a break up of a single line.
Also used are Comparators to compare the names of the Data struct.
PROGRAM CODE:
// use these two headers in your code
#include <vector>
#include <sstream>
// strict data to hold all the three values separately
struct Data
{
string place, name, number;
}
//For sorting the Data, we need a Comparator that compares the names
struct Comparator {
bool operator() (Data data1,Data data2) { return (data1.name<data2.name);}
} comp;
void SortByName()
{
string line;
vector<Data> stringVec;
OpenInputFile();
int i = 0;
SkipHeader();
while(getline(fIn,line))
{
Data data;
istringstream iss(line);
iss>>data.place;
iss>>data.name;
iss>>data.number;
cout<<"line #"<<setw(2)<<left<<i<<" "<<line<<endl;
stringVec.push_back(data);
i++;
}
sort(stringVec.begin(),stringVec.end(), comp);
for (j=0;j<i;j++)
cout << stringVec[j].place << " "<<stringVec[j].name<<" "<<stringVec[j].number<<endl;
fIn.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.