C
Administrator
 Level 90 Ginger
Ballkicks: (+723 / -204)
Posts: 6647 (0.967)
Reg. Date: Jun 2002
Location: Missouri
Gender: Male |
Reply 24 of 42 (Originally posted on: 12-09-10 06:22:26 AM)
Edit Post
| Edit History
| Send PM
| Change Title
| Reply w/Quote
| Report Post
| Ignore
| Show All Posts
Code:
//************************************************************************************** //December 2, 2010 //Program4.ccp // // // //**************************************************************************************
//************************************************************************************** // The include section //**************************************************************************************
#include #include #include
//************************************************************************************** // The namespace section //**************************************************************************************
using namespace std;
//************************************************************************************** // Person Class // This is the Person class which contains all of the information for the class. //**************************************************************************************
class Person {
friend ostream& operator<<(ostream&, Person&); //operator overloads friend istream& operator>>(istream&, Person&); //operator overloads
public: Person(); //constructor Person(Person& obj); //other constructor ~Person(); //destructor void Add(void); //Person::Add void Update(void); //Person::Update void Delete(void); //Person:[img="big grin"]/images/emoticons/biggrin.gif[/img]elete void Print(void); //Person::Print void Sort(void); //Person::Sort void Search(void); //Person::Search void PrintAll(); //Person::PrintAll void DisplayMenu(); //Person:[img="big grin"]/images/emoticons/biggrin.gif[/img]isplayMenu void Quit(); //Person::Quit private: char FirstName[32]; //Variable to hold user input char MiddleName[32]; //Variable to hold user input char LastName[32]; //Variable to hold user input int x; //Variable to utilize key in the class static int Key; //Variable to hold key value Person* LinkedList; //data member that should point //to another instance of the class };
Person::Person() //constuctor for person class { LinkedList = 0; //LinkedList variable initialized to point to null char none[] = {"none"}; //variable to initialize name values with strcpy(FirstName, none); //initializes FirstName value strcpy(MiddleName, none); //initializes MiddleName value strcpy(LastName, none); //initializes LastName value Key++; //increments Key value x = Key; //x takes on value of Key
}
Person::Person(Person& myPerson) //other constructor { //the LinkedList variable in the object //created by this constructor //is supposed to point at myPerson, //and it may or may not do so... this->LinkedList = &myPerson; char none[] = {"none"}; //variable to initialize name values with strcpy(FirstName, none); //initializes FirstName value strcpy(MiddleName, none); //initializes MiddleName value strcpy(LastName, none); //initializes LastName value Key++; //increments Key value x = Key; //x takes on value of Key }
Person::~Person() //destuctor for person class { cout << "Deleted." << endl; system("pause"); }
//************************************************************************************** // Person::Add // This is allows the user to add names to the values. //**************************************************************************************
void Person::Add(void) { char none[] = {"none"}; //holds the defualt value to check the name variables against int result = ((strcmp(none, FirstName)) &&(strcmp(none, MiddleName)) &&(strcmp(none, LastName))); //result holds the check value for the following if statement //the string compares return either a 1 or 0 and result will equal that
if (result == 0) { cin >> *this; } else { cout << "Values already exist please use Update instead." << endl; system("pause"); } } //End of function Person::Add
//************************************************************************************** // Person::Update // This is allows the user to update existing values. //**************************************************************************************
void Person::Update(void) { char none[] = {"none"}; //holds the defualt value to check the name variables against
int result = ((strcmp(none, FirstName)) &&(strcmp(none, MiddleName)) &&(strcmp(none, LastName))); //result holds the check value for the following if statement //the string compares return either a 1 or 0 and result will equal that
if (result != 0) { cout << *this; cout << "Please enter your changes.\\n"; cin >> *this; } else { cout << "No values exist please use Add instead." << endl; system("pause"); } } //End of function Person::Update
//************************************************************************************** // Person:[img="big grin"]/images/emoticons/biggrin.gif[/img]elete // This is allows the user to delete existing values. //**************************************************************************************
void Person:[img="big grin"]/images/emoticons/biggrin.gif[/img]elete(void) { char none[] = {"none"}; //holds the defualt value to check the name variables against
int result = ((strcmp(none, FirstName)) &&(strcmp(none, MiddleName)) &&(strcmp(none, LastName))); //result holds the check value for the following if statement //the string compares return either a 1 or 0 and result will equal that
if (result != 0) { cout << *this; cout << "Are you sure you want to delete the entry? Y/N: "; char yesno; //holds the user input, used by the switch to determine where the program should follow cin >> yesno; char none[] = {"none"}; //holds the defualt value to replace name variables with switch(yesno) { case 'y': case 'Y': cout << "The entry has been deleted.\\n"; strcpy(FirstName, none); strcpy(MiddleName, none); strcpy(LastName, none); system("pause"); break; case 'n': case 'N': cout << "Okay.\\n"; system("pause"); break; default: cout << "\\nPlease select a valid option.\\n"; break; } } else { cout << "No values exist to delete." << endl; system("pause"); } } //End of function Person:[img="big grin"]/images/emoticons/biggrin.gif[/img]elete
//************************************************************************************** // Person::Print // This is allows the user to print the current values. //**************************************************************************************
void Person::Print(void) { char none[] = {"none"}; //holds the defualt value to //check the name variables agianst
int result = ((strcmp(none, FirstName)) &&(strcmp(none, MiddleName)) &&(strcmp(none, LastName))); //result holds the check value for the following if statement //the string compares return either a 1 or 0 and result will equal that
if (result != 0) { cout<<*this; system("pause"); } else { cout << "Person has no values to print." << endl; system("pause"); } } //End of function Person::Print
//************************************************************************************** // Person::Search // This is currently a stub function //**************************************************************************************
void Person::Search(void) { cout << "Search called." << endl; system("pause"); } //End of function Person::Search
//************************************************************************************** // Person::Sort // This is currently a stub function //**************************************************************************************
void Person::Sort(void) { cout << "Sort called." << endl; system("pause"); } //End of function Person::Sort
//************************************************************************************** // Person:[img="big grin"]/images/emoticons/biggrin.gif[/img]isplayMenu // This displays the menu to the screen and calls the other class functions //**************************************************************************************
void Person:[img="big grin"]/images/emoticons/biggrin.gif[/img]isplayMenu() { int Option;
void (Person::*fptr[8])(void) = {&Person::Add, &Person::Update, &Person:[img="big grin"]/images/emoticons/biggrin.gif[/img]elete, &Person::Sort, &Person::Print, &Person::Search, &Person::PrintAll, &Person::Quit}; //array of function pointers
do { system("cls"); cout << "Please select an item from the list (numbers 1-8).\\n" << "1. Add\\n" << "2. Update\\n" << "3. Delete\\n" << "4. Sort\\n" << "5. Print\\n" << "6. Search\\n" << "7. Print All\\n" << "8. Quit\\n"; cin >> Option;
if(Option<1||Option>8) { cout << "Please enter an appropriate option." << endl; system("pause"); } else { (this->*fptr[Option-1])(); } }while(Option!=8); } //End of function Person:[img="big grin"]/images/emoticons/biggrin.gif[/img]isplayMenu
//************************************************************************************** // Person::PrintAll // This doesn't do what it's supposed to do. The function should display all // person objects through the use of pointers (we believe). As is, it displays // the location that the first object points to (000...), and another location // which may or may not be the location of the second object. //**************************************************************************************
void Person::PrintAll() { cout << this->LinkedList << endl; cout << &this->LinkedList << endl; system("pause"); } //End of function Person::PrintAll
//************************************************************************************** // Person::Quit // This function allows the program to quit //**************************************************************************************
void Person::Quit() { } //End of function Person::Quit
//************************************************************************************** //Overloaded Operators //**************************************************************************************
ostream& operator<<(ostream& myOut, Person& Person) //overloads cout { myOut << "Person " //displays the object << Person.x << " is named " << Person.FirstName << " " << Person.MiddleName << " " << Person.LastName << "." << endl; myOut << "Person " //displays what Linked list << Person.x //points to << " has a LinkedList pointing at " << Person.LinkedList << endl; return myOut; }
istream& operator>>(istream& myIn, Person& Person) //overloads cin { cout << "Please enter a first name: "; myIn >> Person.FirstName; cout << "Please enter a middle name: "; myIn >> Person.MiddleName; cout << "Please enter a last name: "; myIn >> Person.LastName;
return myIn; }
//************************************************************************************** //The function prototype section //**************************************************************************************
void StartUp(void); void WrapUp(void);
//************************************************************************************** //The global variable declaration section //**************************************************************************************
char ProgramName[] = "Program3.cpp"; //Used to hold the program name int Person::Key = 0; //initializing Key value
//************************************************************************************** // The enumeration section //**************************************************************************************
enum Status {Success, Failure}; // Used to test or return // value for program // success or failure
//************************************************************************************** // main // This is the entry point for this program. It controls // the flow of execution. //**************************************************************************************
int main() { StartUp();
Person *person1 = new Person; //this instantiates a person object Person *person2 = new Person(*person1); //this instantiates a person object //which should have its LinkedList //variable pointing to the other object person1->DisplayMenu(); delete person1; delete person2;
WrapUp(); return Success; } //End of function main
//************************************************************************************** // StartUp // Currently this is an empty module or stub. It will be used // to perform any needed initialization. //**************************************************************************************
void StartUp(void) { } //End of function StartUp
//************************************************************************************** // WrapUp // It is used to perform any end of program processing needed. //**************************************************************************************
void WrapUp(void) { cout << "Program " << ProgramName << " ended successfully." << endl; } //End of function WrapUp
This thing doesn't do everything it's supposed to, and I can't make it work because I hate programming wheeeeeeee. I'll grab the details, but I figured I'd post the code before I logged off and grab the actual description out of my car around lunch.
"Remember, Jesus would rather constantly shame the gays than let orphans have families."
|