How to read this information from a file and then organize i
How to read this information from a file and then organize it by the numbers given in numerical order in c++
Robinson,Will 000000155 12 Lost in Space
Munster,Herman 000000120 150 1313 Mockingbird Lane, MockingBird Heights
Adams,Gomez 000000111 52 1313 Cementery Lane
Brady,Marcia 000000131 17 4222 Clinton Way, Los Angeles, CA
Cooper,Sheldon 000000165 33 2311 N. Los Robles Avenue, Pasadena, CA
HofStader,Leonard 000000102 33 2311 N. Los Robles Avenue, Pasadena, CA
Farah-Fowler,Amy 000000147 32 Pasadena, CA
Output desired is
HofStader,Leonard 000000102 33 2311 N. Los Robles Avenue, Pasadena, CA
Adams,Gomez 000000111 52 1313 Cementery Lane
Munster,Herman 000000120 150 1313 Mockingbird Lane, MockingBird Heights
Brady,Marcia 000000131 17 4222 Clinton Way, Los Angeles, CA
Farah-Fowler,Amy 000000147 32 Pasadena, CA
Robinson,Will 000000155 12 Lost in Space
Cooper,Sheldon 000000165 33 2311 N. Los Robles Avenue, Pasadena, CA
What I Have so far:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
int numPeople = 0;
string person;
string arrPeople[20];
string tempString;
ifstream peopleIn;
string addPeople;
ifstream myfile (\"a1.txt\");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << \'\ \';
getline(peopleIn, tempString, \',\');
getline(peopleIn, tempString, \' \');
getline(peopleIn, tempString, \' \');
getline(peopleIn, tempString, \' \');
getline(peopleIn, tempString, \'\ \');
arrPeople[numPeople] = person;
++numPeople;
}
while (1)
{
cout << \"Do you want to add another person (y or n): \";
cin >> addPeople;
if (addPeople == \"n\")
break;
cout << \"Enter Last Name: \";
cin >> tempString; // put the data in structure or class
cout << \"Enter First Name: \";
cin >> tempString; //put the data in your structure or class
cout << \"Enter SSN: \";
cin >> tempString; //put the data in your structure or class
cout << \"Enter Address: \";
cin >> tempString; //put the data in your structure or class
cin.ignore(100, \'\ \'); // you may need this to clear the input buffer after entering the address
}
myfile.close();
}
else cout << \"Unable to open file\";
return 0;
}
Trying to make it so i can ask the user if they wish to add another name they hit y or n for yes or no, then change the list like the list above including anything the user puts in. I always seem to have a hard time finding where things are located to manipulate them
Solution
#include<iostream>
#include<string>
using namespace std;
int main () {
string line;
int numPeople = 0;
string person;
string arrPeople[20];
string tempString;
ifstream peopleIn;
string addPeople;
ifstream myfile (\"a1.txt\");
int levels[20];
char p[300];
char arr[100];
int j=0;
if (myfile.is_open())
{
cout << \"\ People before sorting \ \";
while ( getline (myfile,line) )
{
cout << line << \'\ \';
std::strcpy(p, line.c_str());
for(int i=0; i<200;i++) // extract the integer value after first space
{
j=0;
if(p[i]==\' \')
{
i++;
while(p[i]!=\' \') // read all the numbericals upto space
{
arr[j]=p[i];
j++; i++;
}
levels[numPeople]=atoi(arr); // store this value in number array
break;
}
}
arrPeople[numPeople] = line;
++numPeople;
}
// sort the number array and make swaps on both number array as well as string aray
for(int z=0;z<numPeople;z++)
for(int k=z;k<numPeople;k++)
{
if(levels[z]>levels[k])
{
j=levels[z];
levels[z]=levels[k];
levels[k]=j;
person=arrPeople[z];
arrPeople[z]=arrPeople[k];
arrPeople[k]=person;
}
}
cout << \"\ People after sorting \ \";
for(int z=0;z<numPeople;z++)
cout <<arrPeople[z]<< \" \"<<levels[z]<<endl;
myfile.close();
}
else cout << \"Unable to open file\";
return 0;
}


