Question
I'm trying to figure out how to search the file that i have by searching by last name in a list with a persons last
I'm trying to figure out how to search the file that i have by searching by last name in a list with a persons last name first name SSN and address. Which the user can add names to the list first then after they're organized then the program asks if the user wants to search for a persons last name if it finds it, it would print their information out. I'm wondering if there is a way to do this here's an example of what i mean. I'm having a hard time finding something to read just the last names and searching for them.
Wanted output:
do you wish to search for someone (y or n)
then the user would type Robinson then the program would print
Robinson,Will 000000155 12 Lost in Space
The default list of names(this is what's in a1.txt):
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
Code I have to read and organize the data according to SSNs:
#include #include #include #include #include
using namespace std; int main () { ifstream peopleIn; string line; int numPeople = 0; string person; string arrPeople[20]; string tempString; string addPeople; ifstream myfile ("a1.txt");
int levels[20]; char p[300]; char arr[100]; int j=0; if (myfile.is_open()) {
while (1) { cout << "Do you want to add a person to the list? (y or n): "; cin >> addPeople; if (addPeople == "n") break; cout << "Enter Last Name: "; cin >> tempString; // put the data in structure or class ofstream fout("a1.txt", ios::app); fout << tempString; cout << "Enter First Name: "; cin >> tempString; fout << "," + tempString; //put the data in your structure or class cout << "Enter SSN: "; cin >> tempString; //put the data in your structure or class fout << " " + tempString; cout << "Enter Address: "; getline(cin,tempString); //put the data in your structure or class fout << " " + tempString; fout << " "; cin.ignore(100, ' '); // you may need this to clear the input buffer after entering the address arrPeople[numPeople] = person; ++numPeople; } cout << " Unsorted list "; while ( getline (myfile,line) ) { cout << line << ' ';
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 up to 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 for(int k=z;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 << " Sorted List by SSN "; for(int z=0;z cout <
myfile.close(); } else cout << "Unable to open file"; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started