(1)Member object Design and implement a Member object with appropriate data fiel
ID: 3747748 • Letter: #
Question
(1)Member object
Design and implement a Member object with appropriate data fields and methods. Each member should at least contain a CUNYID and a name.
There is no need for VIP Member or Club objects.
(2)Command parsing
The main method must be able to read an input file and execute its commands. At this stage, you can decide the format of the input file yourself. Your program should be able to handle a variety of inputs. It should at least be able to: add a new member, modify a member’s info, and request to output all active member’s information into the output file.
For example, the input file can have a line that says “add 12345678 John Doe”, this would mean to add a new member with CUNY ID = 12345678 and name = John Doe.
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
// For maximum students
#define MAX 10
// Class Members definition
class Members
{
// Data member to store data
string cunyid;
string name;
public:
// Default constructor to initialize data members
Members()
{
cunyid = name = "";
}// End of default constructor
// Parameterized constructor to assign parameter values to data members
Members(string cu, string na)
{
cunyid = cu;
name = na;
}// End of parameterized constructor
// Function to display member information
void display(Members mem[], int len)
{
// Loops till number of members
for(int x = 0; x < len; x++)
// Displays each member information
cout<<" CUNYID: "<<mem[x].cunyid<<" Name: "<<mem[x].name;
}// End of function
// Function to read command and member information and stores it in mem array
int readFile(Members mem[])
{
// To store number of records
int counter = 0;
string command;
string id;
string name;
// ifstream object declared
ifstream fRead;
// Opens the file for reading
fRead.open("Command.txt");
// Checks if the file unable to open for reading display's error message with file name
if(!fRead)
{
cout<<" ERROR: Unable to open the file Command.txt";
exit(0);
}// End of if condition
// Loops till end of the file
while(!fRead.eof())
{
// Extracts command from file
fRead>>command;
// Extracts id from file
fRead>>id;
// Extracts name from file
getline(fRead, name);
// Displays command, id and name
cout<<" "<<command<<" "<<id<<" "<<name;
// Checks if command is "add"
if(command == "add")
{
// Checks if counter is equals to MAX display error message
if(counter == MAX)
cout<<" ERROR: Insufficient memory to add record";
// Otherwise add the record
else
{
// Assigns the id to counter index position of array mem
mem[counter].cunyid = id;
// Assigns the name to counter index position of array mem
mem[counter].name = name;
// Increase the student counter by one
counter++;
}// End of else
}// End of id condition
// Otherwise checks if command is "modify"
else if(command == "modify")
{
// Initial found status is -1
int f = -1;
// Loops till number of records
for(int x = 0; x < counter; x++)
{
// Checks if current index position of array of object mem cunyid is equals to id
if(mem[x].cunyid == id)
{
// Sets the found status to 0
f = 0;
// Displays replace information
cout<<" Replacing "<<name<<" with "<<mem[x].name;
// Assigns the name at x index position of array of objects mem
mem[x].name = name;
// Come out of the loop
break;
}// End of if condition
}// End of for loop
// Checks if f value is -1 then display error message
if(f == -1)
cout<<" ERROR: CUNYID - "<<id<<" not found.";
}// End of else if condition
// Otherwise display error message for command
else
cout<<" ERROR: Invalid command:"<<command;
}// End of while loop
// Closes the file
fRead.close();
// Returns the record counter
return counter;
}// End of function
// Function to write members to file
void writeMember(Members mem[], int len)
{
// ofstream object declared
ofstream fWrite;
// Opens the file for writing
fWrite.open("Members.txt");
// Checks if the file unable to open for writing display's error message with file name
if(!fWrite)
{
cout<<" ERROR: Unable to open the file Members.txt";
exit(0);
}// End of if condition
// Loops till end of the records
for(int x = 0; x < len; x++)
// Checks for the last record
// write the record without new line
if(x == len-1)
fWrite<<mem[x].cunyid<<" "<<mem[x].name;
// Otherwise not last record
// write the record with new line
else
fWrite<<mem[x].cunyid<<" "<<mem[x].name<<endl;
}// End of function
};// End of class
// main function definition
int main()
{
// Declares an array of object of class Members of size MAX
Members member[MAX];
// Calls the function to read data from file and stores it in array of object member
int len = member[0].readFile(member);
// Calls the function to display members information
cout<<" ********** Member Details ********** ";
member[0].display(member, len);
// Calls the function to write members data to file
member[0].writeMember(member, len);
}// End of main function
Sample Output:
add 12345678 John Doe
add 12312378 Pyari Sahu
try 67877745 Anita Panda
ERROR: Invalid command:try
add 67812345 Sasmita Panda
add 67877745 Anita Panda
modify 12345678 Rakesh Sharma
Replacing Rakesh Sharma with John Doe
add 66677744 Sunil Sharma
add 61378855 Binod Padhy
modify 67877745 Dinku Pradhan
Replacing Dinku Pradhan with Anita Panda
add 66678833 Bishnu Sahu
modify 11122233 Ninja Nanda
ERROR: CUNYID - 11122233 not found.
********** Member Details **********
CUNYID: 12345678 Name: Rakesh Sharma
CUNYID: 12312378 Name: Pyari Sahu
CUNYID: 67812345 Name: Sasmita Panda
CUNYID: 67877745 Name: Dinku Pradhan
CUNYID: 66677744 Name: Sunil Sharma
CUNYID: 61378855 Name: Binod Padhy
CUNYID: 66678833 Name: Bishnu Sahu
File Command.txt contents
add 12345678 John Doe
add 12312378 Pyari Sahu
try 67877745 Anita Panda
add 67812345 Sasmita Panda
add 67877745 Anita Panda
modify 12345678 Rakesh Sharma
add 66677744 Sunil Sharma
add 61378855 Binod Padhy
modify 67877745 Dinku Pradhan
add 66678833 Bishnu Sahu
modify 11122233 Ninja Nanda
File Members.txt contents
12345678 Rakesh Sharma
12312378 Pyari Sahu
67812345 Sasmita Panda
67877745 Dinku Pradhan
66677744 Sunil Sharma
61378855 Binod Padhy
66678833 Bishnu Sahu
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.