Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Description Structures provide a way to combine several related variables in

ID: 3601036 • Letter: C

Question

C++

Description

Structures provide a way to combine several related variables into a group. This group of variables can then be manipulated (i.e. transferred to functions) by using a single variable name (single

identifier). For this project an input file will contain several records of personal information.Each record contains several pieces of information about a particular pers on. The order in which the information is stored in the input file needs to be modified; therefore, your program is to read the information from the input file one record at a time and write this information into an output file in the desired order forming a phone book. The records are read and processed one at a time. Therefore, the information for one record is read, processed, output to the output file and then the next record is read. After all records have been read from the input file, the program needs to close all input and output files before terminating.

Restrictions

1) You cannot use arrays (Ch. 11).

2) You are not allowed to use any global variables.

3) Use of global variables will result in a score of 0 on the project.

4) Not using structures will result in a score of 0 on the project

5) If necessary, global constants may be used.

6) Any C++ technique covered in Chapters 1 th rough 10 is allowed. Except as noted above.

7) The input file can be opened and read one time only

8) All user defined functions are called from main only.

9) All information read from the input file must beread into a hierarchical structure variable and the reading must occur in a function.

10)All information written to the output file is to be done from a function – separate from the function that reads the information. The informati on that is to be written must be contained in a structure and passed into the function

Reqirements

1. Global variables are not allowed!! All functions must communicate via their parameter/argument lists.

2.You must at a minimum use 5 void and/or value returning functions (besides main)

3. Format your output as shown in the sample output files.

4.The program should prompt for and read information in the same order as indicated in the sample outputs.

5. main() should consist mainly of function calls. All work performed in this project should be done by functions. All User defined functions are called from main and structures must be used to pass information between main and the functions.

6. A Boolean value returning function can be used to read the information from the input file. A value of true is returned by this function if the input stream is valid, and a value of false is returned if the input stream is in the fail state mode.

7. Use functions to open the input and output files. These functions performs all work related with opening the files. The opening of the files should be fault tolerant. If the name of a non-existent file is entered, a message is printed out and the user is prompted for the name of the file again.

8. Use a loop in main to read a record from the input file(a function call) and then write that information to the output file (a different function call). If a bool value returning function is used to read theinformation, then that return value(which is the status of the file stream) can be used to control the loop.

9. Program needs to detect and output an error message for the case where an error occurs while reading the information from the input file (reference input file P8_in3.txt)

10. Writing to the output file is to take place from inside a function. This function is called after every read from the input file. This function keeps track of the number of entries for a particular phone book by using a static variable. Look at the output generated by the sample solution.

11. The row headings are: Entry, Last Name, First Name, City and Phone Number. All output is left justified and the field width for each column is 7,20,15,20 and 12 respectively. Look at the output produced by the sample solution.

12. When all information has been successfully (no file read error) processed and written to the output file, write another row of column heading dashes to the output file and output to the terminal a message stating successful completion of the program execution.

13. The use of the exit(1); function is to not be used. The program is to terminate from within main(), and termination of the program should only occur after all output files have been closed. Failure to close the output files before termination of the program may result in missing information from the output files.

Explanation / Answer

//program

#include<iostream>

#include<iomanip>

#include<fstream>

using namespace std;

struct info

{

long entry;

char lname[20];

char fname[15];

char city[20];

char phone[12];

};

int main()

{

struct info data;

//input file stream

ifstream in;

//output file stream

ofstream out;

//open input file

in.open("Info28.txt");

if (!in)

{

cout << "Not able to open input file" << endl;

return -1;

}

//open outfile

out.open("InfoOut.txt");

//checl if it;s open

if (!out)

{

cout << "Not able to open output file" << endl;

return -1;

}

//now read each info into struct and write to file

while(!in.eof())

{

in >> data.entry >> data.lname >> data.fname >> data.city >> data.phone;

//now write to file

out << left << setw(7) << data.entry << " " << left << setw(20) << data.lname << " "<<left << setw(15) << data.fname << " "<<left << setw(20) << data.city << " " << left << setw(12) << data.phone << endl;

}

in.close();

out.close();

}

-----------------------------------------------------------

//input file  Info28.txt

1265789 John Smith Newyork 12345678901
5654327 Donald ral Chicago 8765543218

//output file InfoOut.txt

1265789 John Smith Newyork 12345678901
5654327 Donald ral Chicago 8765543218