We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Passing Linked list into function

rsissick
rsissick asked
on
Medium Priority
763 Views
Last Modified: 2013-11-17
I have a homework assignment to write an address list using a linked list.  I am having a problem passing the list into a function.  I know I want to pass by reference.  in my menu program below, I pass the list into the function "newentry", but when I return to the menu and print the list it is empty.

Please help.
#include <iostream>
#include <string>
using namespace std;
#include "menu.h"
 
void main()
{
	mainmenu();
}
 
void mainmenu()
{
	SortedType list;
	AddrType address;
	int select;
 
	cout << "Enter your selection: " << endl << endl;
	cout << "(1) New entry " << endl;
	cout << "(2) Delete an entry " << endl;
	cout << "(3) Edit an entry " << endl;
	cout << "(4) Generate a birthday card " << endl;
	cout << "(5) Generate an aniversary card " << endl;
	cout << "(6) Print List " << endl;
	cout << "(7) Exit program " << endl;
 
	cin >> select;
 
 
	switch (select)
	{
		case 1:
			//cout << "New Entry" << endl;
			newentry(list);
			break;
		case 2:
			cout << "Delete an entry" << endl;
			break;
		case 3:
			cout << "Edit an entry" << endl;
			break;
		case 4:
			cout << "Generate BDay Card" << endl;
			break;
		case 5:
			cout << "Gen Aniversay Card" << endl;
			break;
		case 6:
			//cout << "Print List" << endl;
			PrintList(list);
			break;
		case 7:
			cout << "Exit" << endl;
			break;
		default:
			cout << "Enter a number from 1-6: " << cout << endl;
			mainmenu();
	}
}
void newentry(SortedType& list)
{
	string fname;
	string lname;
	string addr;
	int bday;
	int aniv;
	AddrType address;
	cout << "Enter First Name: ";
	cin >> fname;
	address.initfname(fname);
	cout << fname << endl;
	cout << "Enter Last Name: ";
	cin >> lname;
	address.initlname(lname);
	cout << lname << endl;
	cout << "Enter Address: ";
	fflush(stdin);
	getline(cin, addr);
	address.initaddress(addr);
	cout << addr << endl;
	cout << "Enter Birthday: ";
	cin >> bday;
	address.initbday(bday);
	cout << bday << endl;
	cout << "Enter Aniversary: ";
	cin >> aniv;
	address.initaniv(aniv);
	cout << aniv << endl;
	list.InsertItem(address);
/*
	int length;
	AddrType address2;
	list.ResetList();
	cout << "List" << endl;
	length = list.GetLength();
	for (int counter = 1; counter <= length; counter++)
	{
		list.GetNextItem(address2);
		address2.PrintFirst(cout);
		cout << endl;
		address2.PrintLast(cout);
		cout << endl;
		address2.PrintBday(cout);
		cout << endl;
		address2.PrintAniv(cout);
		cout << endl;
	}
*/
 
	mainmenu();
 
}
void PrintList(SortedType &list)
{
	int length;
	AddrType address;
	list.ResetList();
	cout << "List" << endl;
	length = list.GetLength();
	for (int counter = 1; counter <= length; counter++)
	{
		list.GetNextItem(address);
		address.PrintFirst(cout);
		cout << endl;
		address.PrintLast(cout);
		cout << endl;
		address.PrintBday(cout);
		cout << endl;
		address.PrintAniv(cout);
		cout << endl;
	}
}

Open in new window

Comment
Watch Question

AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
void PrintList(SortedType &list)
{
        int length;
        AddrType address;
        list.ResetList();       <<---------------------------


Does that empty the list?  

Author

Commented:
No, but I kinda figured someone would ask me that.  It just resets the current pos pointer to NULL.  

Thanks

void SortedType::ResetList()
{
  currentPos = NULL;
}

void SortedType::GetNextItem(AddrType& item)
{
  if (currentPos == NULL)
    currentPos = listData;
  item = currentPos->info;
  currentPos = currentPos->next;

}
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
void newentry(SortedType& list)
{
...
mainmenu();  <<---  This will generate a NEW list.

You need a loop in the main function
Senior Software Engineer (Avast)
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
Andy, it seems we simul-posted :)
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
@evilrix - No problems

Author

Commented:
I understand.  I want the menu to be displayed after the new item is entered.  How do I do this without calling my menu function again?
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank so much!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.