Question

Objective: Learn how to -- read string from a file -- write multiple files Problem: Modify...

Objective: Learn how to

-- read string from a file

-- write multiple files

Problem: Modify the Person class in Lab #4. It has four private data members firstNam (char[20]), day, month, year (all int); and two public member functions: setPerson(char *, int, int, int) and printInfo(). The function setPerson sets the first name and birthday in the order of day, month and year. The function printInfo prints first name and birthday. But it should print the date in the form: March 12, 2019 if the inputted string is 03/12/2019. You must separate three files: put the class definition and const months in a header file, put the definition of the member functions in a cpp file. Prepare a file called namelist.txt and type some names and birthdays with format of John 12/06/1996 in the file. In the main program, declare an array of objects with type Person and 10 elements, then use for or while loop to repeatedly read one line of words from the file, call strtok_s to separate the first name, day, month and year, and call setPerson to set the data members for each element (you may assume each line in the file has exact two words, so you don’t need to verify them. After finishing reading all names in the files, call the printInfo() function for each element which has values.

Requirements: Must use multiple files.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the Code according to the given problem.

Prerequisite:make textfile to read.

Header File:

a.h

class Person
{ private:
char[20] firstNam;
   int day;
   int month;
   int year;
    public:
       setPerson(char *, int, int, int);
           printInfo();
};

CPP FILE

a.cpp

#include "a.h"

public void setPerson(String name,int d,int m,int y)
   {
       firstNam=name;
       day=d;
       month=m;
       year=y;
   }
   public void printInfo()
   {
       cout<<"Name"<<firstNam<<endl;
       cout<<"Birthday"<<day<<"/"<<month<<"/"<<"year"<<endl;
   }

MAIN FILE

main.cpp

#include<iostream>
#include<fstream>
#include<string>
#include "a.h"
using namespace std;

int main()
{
   int i,j=0;
   Person p[10];
   ifstream myfile ("namelist.txt");
   String line;
  
if (myfile.is_open())
{

   while(getline(myfile, line, ' '))
{
tokens.push_back(line);
}
  
  
for(int i = 0; i < 10; i++)
{
  
   p[i].setPerson(tokens[j],tokens[j+1],tokens[j+2],tokens[j+3])
   p[i].printInfo();
}
  
myfile.close();
}

else cout << "Unable to open file";

return 0;
   }
  
}

Hope Solution is understandable and please give the proper feedback so I can Improve Myself.

Add a comment
Know the answer?
Add Answer to:
Objective: Learn how to -- read string from a file -- write multiple files Problem: Modify...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • write program in C language. To get more practice working with files, you will write several...

    write program in C language. To get more practice working with files, you will write several functions that involve operations on files. In particular, implement the following functions 1. Write a function that, given a file path/name as a string opens the file and returns its entire contents as a single string. Any endline characters should be preserved char *getFileContents (const char filePath); 2. Write a function that, given a file path/name as a string opens the file and returns...

  • C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the r...

    C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the required information. Your function should return the number of student entries read from the file. Empty lines do not count as entries, and should be ignored. If the input file cannot be opened, return -1 and do not print anything.     Your function should be named...

  • It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #inclu...

    It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #include "File.h" // Constructor of File that takes File name and type as arguments File::File(string type, string name) {    this->type = type;    this->name = name; } //returns the type. string File::getType() {    return type; } //returns the name of file. string File::getName() {    return name; } File.h #ifndef __FILE_H__ #define...

  • Objectives: 1. Classes and Data Abstraction?2. User-defined classes?3. Implementation of a class in separate files Part...

    Objectives: 1. Classes and Data Abstraction?2. User-defined classes?3. Implementation of a class in separate files Part 1: In this assignment, you are asked: Stage1:?Design and implement a class named memberType with the following requirements: An object of memberType holds the following information: • Person’s first name (string)?• Person’s last name (string)?• Member identification number (int) • Number of books purchased (int)?• Amount of money spent (double)?The class memberType has member functions that perform operations on objects of memberType. For the...

  • IN C++ Write a program in c++ that will read from a file the following sentence:...

    IN C++ Write a program in c++ that will read from a file the following sentence: The quick brown fox jumps over the lazy dog Each word must be read into one location in an array beginning with the first element. You must declare an array as follows: char *words [9] ; // this is an array of c- strings. HINT words[0] will contain "the" words[1] will contain "quick" write a function int length (const char *a) to determine the...

  • Implement the following class in interface and implementation files. A separate file (the main project file)...

    Implement the following class in interface and implementation files. A separate file (the main project file) shall have the main function that exercises this class. Create a class named Student that has three member variables: name - string that stores the name of the student numClasses - integer that tracks how many courses the student is currently enrolled in, this number must be between 1 and 100 ClassList - an array of strings of size 100 used to store the...

  • //Done in C please! //Any help appreciated! Write two programs to write and read from file...

    //Done in C please! //Any help appreciated! Write two programs to write and read from file the age and first and last names of people. The programs should work as follows: 1. The first program reads strings containing first and last names and saves them in a text file (this should be done with the fprintf function). The program should take the name of the file as a command line argument. The loop ends when the user enters 0, the...

  • In C++ Write a program that will read a string, call 2 functions to modify the...

    In C++ Write a program that will read a string, call 2 functions to modify the string, and then print the final result. The first function should take a string parameter and return the string without any vowels. The second function should take a string and double every letter (which should be all consonants at this point). Be sure to: You must use more than [ ] and at You must use erase, insert, replace, find, and/or substr to make...

  • Reading and Writing Complete Files in C: The first part of the lab is to write...

    Reading and Writing Complete Files in C: The first part of the lab is to write a program to read the complete contents of a file to a string. This code will be used in subsequent coding problems. You will need 3 functions: main(), read_file() and write_file(). The main function contains the driver code. The read_file() function reads the complete contents of a file to a string. The write_file() writes the complete contents of a string to a file. The...

  • Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read...

    Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read and process a file containing customer purchase data for books. The books available for purchase will be read from a separate data file. Process the customer sales and produce a report of the sales and the remaining book inventory. You are to read a data file (customerList.txt, provided) containing customer book purchasing data. Create a structure to contain the information. The structure will contain...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT