Question: The program Lab4.cpp reads contents from input file, which is sample.txt in todays lab. Each row of the input file has a leading char, followed

The program Lab4.cpp reads contents from input file, which is sample.txt in todays lab. Each row of the input file has a leading char, followed by a string of SSN, and first name and last name. The program stores SSN and the corresponding name (including both first name and last name) to an array. After the whole input file is processed, the program prompts the user to type a SSN, then it will search the SSN in the array. If there is a match, the program prints out the index value of the entry. For example,

jli$ ./a.out sample.txt

Input a SSN: 766434955 Found at location 4

In this lab, you need to use fstream library to read file. An example code is listed as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include

#include

using namespace std;

int main(int argc, char* argv[]){

int x, y, z; fstream input(argv[1]);

while(!input.eof()){

input >> x >> y >> z;

if(!input) break; }

input.close(); }

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In the above example, - the file name is stored in argv[1]

- eof() checks whether we are reaching the end of file. If yes, it returns true; otherwise, false. eof() is similar to hasnext() method in Java.

- each row of the input file has three integers, the while loop reads these three integers and stores them in variables x, y, and z

- it is a good idea to close the file if you do not need it any more

The contents of sample.txt file is found below:

i 545309496 JANNET THOMURE i 912741495 LIZABETH HOSNER i 173421651 NADIA KROPIDLOWSKI i 815904565 KATHYRN BERRINGER i 766434955 NAKIA PEASLEY i 478362801 SHERILL HYLLE i 138863035 ALINE BAIO i 493582998 LORETA BLESER i 038249140 NORMAND DODSON i 652802112 LATANYA ODENWALD i 926654750 RAMONA FILBURN i 151682139 JAMEY SILCOX i 364523152 IRMGARD SEWALL

REQUIREMENTS

- Define a struct, which has two string variables SSN and name.

- Use the struct to statically define an array with size 1000.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!