Hey guys,

I'm writing a program for a class that basically is a menu that allows you to add people, delete people, change info, locate them by ID and last name, print the list, load the list from a file, and save the list to a file (in that order in the code). I have the code written and it is working fine. The only problem is that the professor stipulates that it must be passing each of these to a function instead of being in main as it is written now. Every time I pass it to a function it seems to work but doesn't actually save the changes. I know I should pass it as a pointer, but for some reason it still doesn't seem to be doing anything. Here is the code I have right now (sorry for it being a little lengthy). If I've left out any necessary information please let me know, and thanks in advance for your help/time!

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <list>
#include <strstream>
#include <iterator>
#include "person.h"
#include <new>
using namespace std;

void showMenu()
{
	cout << setw(5) << "MENU" << endl;
	cout << setw(2) << "1.  Add a person to the list" << endl;
	cout << "2.  Delete a person from the list" << endl;
	cout << "3.  Change a person's information" << endl;
	cout << "4.  Locate a person by ID number" << endl;
	cout << "5.  Locate a person by last name" << endl;
	cout << "6.  Print the list on the screen" << endl;
	cout << "7.  Load the list from a file" << endl;
	cout << "8.  Save the list to a file" << endl;
	cout << "9.  Exit the program" << endl << endl;
}


int main(int argc, char argv[])
{
	Person* head = new Person;
	head = NULL;
	int directory = 0;
	while(1)
	{
	showMenu();
	cout << "Choose an action: ";
	cin >> directory;
	cout << endl;
	switch(directory)
	{
		case 1:
			{
				Person *newNode;
				Person *nodePtr;
				newNode = new Person;
				char tempLN[30];
				char tempFN[30];
				char tempID[30];
				cout << "Enter the person's last name: ";
				cin >> tempLN;
				cout << "Enter the person's first name: ";
				cin >> tempFN;
				cout << "Enter the person's ID number: ";
				cin >> tempID;
				cout << endl << "--PERSON ADDED--\n" << endl;
				newNode->setLastName(tempLN);
				newNode->setFirstName(tempFN);
				newNode->setZipCode(tempID);
				newNode->pNext = NULL;

				if(!head)
				{
					head = newNode;
				}
				else
				{
					nodePtr = head;
					while(nodePtr->pNext)
					{
						nodePtr = nodePtr->pNext;
					}
					nodePtr->pNext = newNode;
				}
			}
			break;
		case 2:
			{
				string temp;
				cout << endl << "Enter the employee ID of the employee to delete: ";
				cin >> temp;
				cout << endl;

				int itemFound = 0;
				Person* currentItem = head;
				Person* previousItem = currentItem;
				while(currentItem)
				{
					if(currentItem->getZipCode() == temp )
						{
							if(previousItem==currentItem)
								head = currentItem->pNext;
							else
								previousItem->pNext = currentItem->pNext;
							cout << "item: " << currentItem->getFirstName() << " " << currentItem->getLastName() << " deleted!" << endl;
							delete currentItem;
							itemFound = 1;
							break;
						}
					previousItem = currentItem;
					currentItem = currentItem->pNext;
				}
				if(itemFound==0)
					cout << "Value not found in the list!" << endl;
				cout << endl;
			}
			break;
		case 3:
			{
				string temp4;
				char firstName[30];
				char lastName[30];
				int nameChange;
				int itemFound = 0;
				cout << endl << "Enter the last name of the employee you would like to change the information of: ";
				cin >> temp4;
				cout << endl;
				Person* currentItem = head;
				Person* previousItem = currentItem;
				while(currentItem)
				{
					if(currentItem->getLastName() == temp4)
					{
						cout << "Enter 1 to change their first name or 2 to change their last name: ";
						cin >> nameChange;
						cout << endl;
						if(nameChange == 1)
						{
							cout << "Enter the new first name: ";
							cin >> firstName;
							currentItem->setFirstName(firstName);
							cout << endl << "The person's new name is " << currentItem->getFirstName() << " " << currentItem->getLastName();
							cout << endl;
						}
						else if(nameChange == 2)
						{
							cout <<"Enter the new last name: ";
							cin >> lastName;
							currentItem->setLastName(lastName);
							cout << endl << "The person's new name is " << currentItem->getFirstName() << " " << currentItem->getLastName();
							cout << endl;
						}
						else
						{
							cout << "Not a valid selection" << endl;
						}
						itemFound = 1;
					}
					previousItem = currentItem;
					currentItem = currentItem->pNext;
				}
				if(itemFound == 0)
					cout << "No one by that last name was found in the records!" << endl;
			}
			break;
		case 4:
			{
				string temp2;
				cout << endl << "Enter the employees ID number to locate them: ";
				cin >> temp2;
				cout << endl;
				int itemFound = 0;
				Person* currentItem = head;
				Person* previousItem = currentItem;
				while(currentItem)
				{
					if(currentItem->getZipCode() == temp2)	
					{
						cout << "The employees name is " << currentItem->getFirstName() << " " << currentItem->getLastName() << endl << endl;
						itemFound = 1;
					}
					previousItem = currentItem;
					currentItem = currentItem->pNext;
				}
				if(itemFound == 0)
					cout << "No employee by that ID was found in the records" << endl << endl;
			}
			break;
		case 5:
			{
				string temp3;
				cout << endl << "Enter the employees last name to locate them: ";
				cin >> temp3;
				cout << endl;
				int itemFound = 0;
				Person* currentItem = head;
				Person* previousItem = currentItem;
				while(currentItem)
				{
					if(currentItem->getLastName() == temp3)
					{
						cout << "The employees ID number is " << currentItem->getZipCode() << "." << endl << endl;
						itemFound = 1;
					}
					previousItem = currentItem;
					currentItem = currentItem->pNext;
				}
				if(itemFound == 0)
					cout << "No employee by that last name was found in the records" << endl << endl;
			}
			break;
		case 6:
			{
			    Person* currentItem = head;
				cout << "DIRECTORY: " << endl << endl;
				while(currentItem)
				{
					cout << "Name: " << currentItem->getFirstName() << " " << currentItem->getLastName() << endl;
					cout << "ID Number: " << currentItem->getZipCode() << endl << endl;
					currentItem = currentItem->pNext;
				}
			}
			break;
		case 7:
			{
				Person* currentItem = head;
				if(currentItem!=0)
				{
					currentItem = head;
					while(currentItem)
					{
						Person* tempPerson = new Person;
						tempPerson = currentItem;
						currentItem = currentItem->pNext;
						delete tempPerson;
					}
					head = 0;
				}

				cout << endl << "List will be loaded from file after you press any key . . ." << endl;
				system("pause");
				cout << endl;

				ifstream file2;
				file2.open("MYLIST.TXT");

				if(file2 == 0)
				{
					cout << "error opening MYLIST.TXT!" << endl;
					system("pause");
					return 1;
				}

				while(!file2.eof())
				{
					int remainder;
					int counter = 1;
					const int SIZE = 30;
					char temp_fn[SIZE];
					char temp_ln[SIZE];
					char temp_zp[SIZE];
					Person* newPerson = new Person;
					while((remainder=counter%4)!=0)
					{
						if((remainder=counter%3) == 0)
						{
							file2.getline(temp_zp, SIZE);
							newPerson->setZipCode(temp_zp);
						}
						else if((remainder=counter%2) == 0)
						{
							file2.getline(temp_ln, SIZE);
							newPerson->setLastName(temp_ln);
						}
						else
						{
							file2.getline(temp_fn, SIZE);
							newPerson->setFirstName(temp_fn);
						}
						counter++;
					}
					newPerson->pNext = head;
					head = newPerson;
				}
		Person* nodePtr;
		nodePtr = head->pNext;
		delete head;
		head = nodePtr;
		break;
			}
		case 8:
			{
				Person* currentItem = head;
				if(currentItem==0)
					cout  << endl << "Empty list! No file created!" << endl;
				else
				{
					ofstream file1;
					file1.open("MYLIST.TXT");
					if(file1 == 0)
					{
						cout << "error opening MYLIST.TXT" << endl;
						return 1;
					}
					while(currentItem)
					{
						file1 << currentItem->getFirstName() << endl;
						file1 << currentItem->getLastName() << endl;  
						file1 << currentItem->getZipCode() << endl;
						currentItem = currentItem->pNext;
					}
					cout << "\n-- LIST SAVED --\n\n";
					file1.close();
				}
			}
			break;
		case 9:
			exit(0);
		default:	
			cout << "Not a valid selection" << endl;
			break;
	}
	}
	system("pause");
}

I know I should pass it as a pointer, but for some reason it still doesn't seem to be doing anything.

How do you do this? Could you post an example? Mind that you may have to pass the pointer by reference (or by pointer). Check this out:

#include <iostream>

using namespace std;

void f1(int *   p) {  p = (int *) 0xff; }
void f2(int * & p) {  p = (int *) 0xff; }
void f3(int * * p) { *p = (int *) 0xff; }

int main()
{
    int * p1 = 0;
    int * p2 = 0;
    int * p3 = 0;

    cout << "p1 = " << p1 << endl;
    cout << "p2 = " << p2 << endl;
    cout << "p3 = " << p3 << endl;

    cout << "\ncalling f1, f2, f3..." << endl;

    f1(p1); f2(p2); f3(&p3);

    cout << "\np1 = " << p1 << endl;
    cout <<   "p2 = " << p2 << endl;
    cout <<   "p3 = " << p3 << endl;

    cout << "\n(hit enter to quit...)"; cin.get();

    return 0;
}

Edited 5 Years Ago by m4ster_r0shi: n/a

Well, if instead I make my function something like this:

void addPerson(Person *head)
{
				Person *newNode;
				Person *nodePtr;
				newNode = new Person;
				char tempLN[30];
				char tempFN[30];
				char tempID[30];
				cout << "Enter the person's last name: ";
				cin >> tempLN;
				cout << "Enter the person's first name: ";
				cin >> tempFN;
				cout << "Enter the person's ID number: ";
				cin >> tempID;
				cout << endl << "--PERSON ADDED--\n" << endl;
				newNode->setLastName(tempLN);
				newNode->setFirstName(tempFN);
				newNode->setZipCode(tempID);
				newNode->pNext = NULL;

				if(!head)
				{
					head = newNode;
				}
				else
				{
					nodePtr = head;
					while(nodePtr->pNext)
					{
						nodePtr = nodePtr->pNext;
					}
					nodePtr->pNext = newNode;
				}
			}

and then call it in case 1: like

case 1: 
      addPerson(head);
      break;

It will run just as it normally would but when I print it there is nothing there.

That is what I'm trying to fix. I figure it is a problem with a pointer reference somewhere. Thank you for your speedy reply.

Edited 5 Years Ago by fatalaccidents: n/a

Yes, it's what I thought it is. Make the signature of your function -> void addPerson(Person * & head) and it should be ok.

The problem with void addPerson(Person * head) is that, while you can modify the data pointed to by head ,
you can't modify head itself. And you want to be able to modify head here (at least) -> head = newNode;

Edited 5 Years Ago by m4ster_r0shi: n/a

Thank you very much Roshi, this worked like a charm. I imagine this is because it is pointing to the actual location of head instead of just pointing to head? I think with this I'll be able to figure it out from here. Many, many thanks.

This question has already been answered. Start a new discussion instead.