Question
Problem: Modify your code (which is posted below) by adding additional two data age with int data type. Define the class Person in Person.h file
Problem: Modify your code (which is posted below) by adding additional two data age with int data type. Define the class Person in Person.h file and define four member functions in Person.cpp. The function setName sets the first name, last name and age. The function printName prints last name followed by a comma, first name, and age both to a file and to screen. You need to pass an object of ofstream type to write data to a file. You also need to open notepad to type 5 full names (any names and numbers are OK):
John Smith 24
William Derrick 32
Chris Lee 19
Anna Budd 31
Joe Grant 25
Save it with the file name infile.txt. You can assume each line has three words and the last word is a number. In the main program, declare an array of objects of type Person with 20 elements. Next, repeatedly read the full name one line by one line, separate the first, last name and age and call setName() to assign some data to the objects. If your input file has more than 20 full names, stop reading and output message Too many names. Stop reading. Then output all names both to screen and to a file.
Requirements: You must use three files. Your program can read up to 20 names.
Note: When you use strtok to get the word 24, it is a string. You must call the function atoi to convert a string to an integer.
Suggested screen design (the output file also has the same messages):
Last Name First Name Age
Smith, John 24
Derrick, William 32
Lee, Chris 19
Budd, Anna 31
Grant, Joe 25
----------------------------------------------------------------------------
#include
#include
#include
class Person { public: void setName(char *fName, char *lName, char a) { strcpy_s(firstName, fName); strcpy_s(lastName, lName); initial = a; } void printName() { cout << setw(14) << lastName << ","; cout << setw(16) << firstName; cout << setw(18) << initial << endl; } private: char firstName[20], lastName[20], initial; };
int main() { const int size = 10; Person p[size]; char fullName[80], *fName, *lName, *mName; char *nextPtr; int i; for (i = 0; i < size; i++) { cout << "Enter Full name for person "<< i + 1 << " (exit to end:) " ; cin.getline(fullName,79,' '); fName = strtok_s(fullName, " ", &nextPtr); if (strcmp(fullName, "exit") == 0) break; else { if (fName == NULL) { cout << "You didn't type name for person " << i + 1 << ". Try again. "; cout << endl; i--; continue; } lName = strtok_s(NULL, " ", &nextPtr); if (lName == NULL) { cout << "You didn't type name for person " << i + 1 << ". Try again. "; cout << endl; i--; continue; }
mName = strtok_s(NULL, " ", &nextPtr);
if (mName == NULL) p[i].setName(fName, lName, '-');
else { p[i].setName(fName, mName, lName[0]); cout << endl;
} } } cout << endl; cout << setw(15) << "last Name"; cout << setw(16) << "First Name"; cout << setw(17) << " Middle Initial" << endl; for (int j = 0; j < i; j++) p[j].printName(); 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