Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Passing Linked list into function

Posted on 2009-07-04
9
738 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

0
Comment
Question by:rsissick
  • 4
  • 3
  • 2
9 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 24776802
void PrintList(SortedType &list)
{
        int length;
        AddrType address;
        list.ResetList();       <<---------------------------


Does that empty the list?  

0
 

Author Comment

by:rsissick
ID: 24777346
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;

}
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 24777374
void newentry(SortedType& list)
{
...
mainmenu();  <<---  This will generate a NEW list.

You need a loop in the main function
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 40

Accepted Solution

by:
evilrix earned 250 total points
ID: 24777392
newentry calls mainmenu at the end, so it's not returning it's actually starting a new callstack, which creates a new version of your list in that callstack context. You do the same in the dwefault of your switch. You'll eventually get a stack overflow doing this.

See my comments below...
#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(); //<---------------------------------------------RX: [ REMOVE THIS ]
        }
}
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(); //<---------------------------------------------RX: [ REMOVE THIS ]
 
}
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

0
 
LVL 40

Expert Comment

by:evilrix
ID: 24777395
Andy, it seems we simul-posted :)
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 24777423
@evilrix - No problems
0
 

Author Comment

by:rsissick
ID: 24782148
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?
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 250 total points
ID: 24782906
As I said - you use a loop
(ps. For homework we are limited with what we can write due to EE rules)

foo()
{
  MyList list;
  while(TRUE)
  {
    //display options
    //process input
    foo2(list);    //here the ONE instance of list is processed
  }
}
0
 

Author Closing Comment

by:rsissick
ID: 31599760
Thank so much!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question