So I am trying to wrap my head around link lists and the theory is there but the magic isn't (at least in my program)... my problem runs with the void traverseRecord() function. For some reason it automatically shows all the nodes.
Maybe I haven't totally grasped the concept of traversing, because I am thinking it will only show one at a time and I know my code isn't exactly right. Ideas anyone?

// studentGPAquery.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <list>

using namespace std;

struct student
{
	string first_name;
	string last_name;
	double GPA;

	student(){}
	~student(){}
	struct student *next;
};

student *head = NULL;

void mainMenu()
{
	cout<<endl;
	cout<<"++++++++++++++++++++++++++++++++++"<<endl;
	cout<<"            MAIN MENU             "<<endl;
	cout<<"    Choose from the following:    "<<endl;
	cout<<"[N]umber of students in the class "<<endl;
	cout<<"[A]dd a record                    "<<endl;
	cout<<"[D]elete a record                 "<<endl;
	cout<<"[V]iew all records                "<<endl;
	cout<<"[T]raverse all records            "<<endl;
	cout<<"[Q]uit program                    "<<endl;
	cout<<"++++++++++++++++++++++++++++++++++"<<endl;
	cout<<endl;
}

void addRecord()
{
	cout<<endl;
	cout<<"Add record:"<<endl;
	cout<<endl;

	struct student *temp, *alt;
	temp = new student;

	cout<<"What is the student's last name: ";
	cin>>temp->last_name;
	cout<<"What is the student's first name: ";
	cin>>temp->first_name;
	cout<<"What is student's GPA: ";
	cin>>temp->GPA;
	temp->next = NULL;

	if(head == NULL)
		head = temp;
	else
	{
		alt = head;
		while(alt->next != NULL)
		{
			alt = alt->next;
		}
		alt->next = temp;
	}
}

void deleteRecord()
{
	cout<<endl;
	cout<<"Delete record:"<<endl;
	cout<<endl;

	student *del;
	del = head;
	del = del->next;

	if(del->next == NULL)
		cout<<"End of the list."<<endl;
	else
		del = del->next;

	if(del == head)
		cout<<"Beginning of list."<<endl;
	else
	{
		student *prev;
		prev = head;
		while(prev->next != del)
		{
			prev = prev->next;
		}
		del = prev;
	}
	cout<<endl;
	cout<<"Name to delete: ";
	cin>>del->last_name;
	
	if(del->next == NULL)
		cout<<"Nothing follows."<<endl;
	else
	{
		student *temp;
		temp = del->next;
		del->next = temp->next;
		delete temp;
	}
}

void viewRecord()
{
	cout<<endl;
	cout<<"All records:";
	cout<<endl;

	struct student *temp;
	temp = head;

	do
	{
		if(temp == NULL)
			cout<<"Nothing follows."<<endl;
		else
		{
		cout<<"Name: "<<temp->last_name<<", ";
		cout<<temp->first_name<<endl;
		cout<<"GPA: "<<temp->GPA<<endl;
		cout<<endl;
		temp = temp->next;
		}
	}
	while(temp != NULL);
}
void traverseRecord()
{
	struct student *temp;
	temp = head;
	if(temp != 0)
	{
		while(temp != 0)
		{
			cout<<"Name: "<<temp->last_name<<", ";
			cout<<temp->first_name<<endl;
			cout<<"GPA: "<<temp->GPA<<endl;
			cout<<endl;
			temp = temp->next;
		}
	}
	while(temp != NULL)
	{
		cout<<"Name: "<<temp->last_name<<", ";
		cout<<temp->first_name<<endl;
		cout<<"GPA: "<<temp->GPA<<endl;
		cout<<endl;
		temp = temp->next;
	}

}

int _tmain(int argc, _TCHAR* argv[])
{
	char choice;
	
	do{
		mainMenu();
			cout<<"Command?: ";
			if(cin>>choice)
				switch(choice)
				{
				case 'A':
					addRecord();
					break;
				case 'D':
					deleteRecord();
					break;
				case 'V':
					viewRecord();
					break;
				case 'T':
					traverseRecord();
					break;
				}
	}
	while(choice != 'Q');
		cout<<endl;
		cout<<"Terminating.."<<endl;

	system("PAUSE");
	return 0;
}

My other issue is sorting by last name, but I haven't reached that point yet in my code (one hurdle at a time)... but concept suggestions are welcome.

What I have lined up for sorting is:

left.last_name < right.last_name;

I'm still trying to figure out how to implement it.

~Climber Ty

For some reason it automatically shows all the nodes.
Maybe I haven't totally grasped the concept of traversing, because I am thinking it will only show one at a time and I know my code isn't exactly right.

Well, what do you WANT the "traverse" function to do? I would expect and desire a "traverse" function to go through the entire list and display it, so from your description it sounds like it's working the way I would want it to.

It shows all the records, "one at a time". This isn't a contradiction to my way of thinking.

So frankly I'm confused about exactly where the program isn't working. I don't know the purpose of the "Traverse" option. You already have a "View all records" option. I say delete the "Traverse" option altogether.

As for how to sort, you'll want to write a comparison function that checks whether one record is "less than" another record, then sort based on that.

VernionDozer,

You're right, I guess I already solved that. Thank you! Maybe I just needed some outside clarification on the whole traverse function.

Anyway I'll take care of the sorting issue, I'd rather write my own and have someone edit, then to just implement code given to me.

Though there is one more thing... I was testing this last night and I was having weird feedback from the delete function...

When I have two or more names/GPA are added to the list and upon viewing them, all is well.
When I try to delete a record by last name, the wrong one is deleted.

For example:

Command?: V

All records:
Name: Smoe, Joe
GPA: 4.3

Name: Snuffy, Mike
GPA:3.9

Name: Doe, Jon
GPA: 3.0

//+++++++++++  (I'm just showing a shortened version of the menu)
// MAIN MENU
//+++++++++++

Command?: D

Delete record:

Name to delete: Doe

//+++++++++++  (I'm just showing a shortened version of the menu)
// MAIN MENU
//+++++++++++

Command?: V

All records:
Name: Smoe, Joe
GPA: 4.3

Name: Doe, Jon
GPA: 4.6

The other interesting thing is if I add another name and then delete it. The first name is deleted but the last name is bumped to the previous entry, replacing that last name.
Also if I make a record of two or more then try to delete the initial record, the last name somehow replaces the second last name in the record, deletes the third record, and maintains the the initial record.... odd.

Command?: V

All records:
Name: Smoe, Joe
GPA: 4.3

Name: Snuffy, Mike
GPA:3.9

Name: Doe, Jon
GPA: 3.0

//+++++++++++  (I'm just showing a shortened version of the menu)
// MAIN MENU
//+++++++++++

Command?: D

Delete record:

Name to delete: Smoe
//+++++++++++  (I'm just showing a shortened version of the menu)
// MAIN MENU
//+++++++++++

Command?: V

All records:
Name: Smoe, Joe
GPA: 4.3

Name: Smoe, Mike
GPA: 3.9

I think it has something to do with each list link order, but I can't figure out why the last names are being altered.

I'm going to have to go over my code again, maybe I misplaced a command in there somewhere. Any suggestions?

-Climber Ty

You're welcome. By the way "traverse" just means "go through the list". You could be doing that for any number of reasons (print all records, find a record, sort records, etc.).

Your delete function looks way off. I can't see any way for it to work given that you first FIND the node to delete, THEN ask for the name. Seems to me you need to ask for the name first, THEN find the node, then delete the node.

I think I am going about this all wrong... after trying to fiddle around with some more options I blieve I have totally lost control of the program.

As I mention above with the deleting problem, I think my error lies in the fact that neither first_name and last_name are constructed as one node. Because if I edit my delete section to just delete last_name... only the last_name goes, first_name stays.

I think it's time to go back to the old drawing board and start anew.

I totally redid my code from scratch and this time it worked... thanks VernonDozier for your help!

Ideas anyone?

commented: At 9 years, time to start your new discussion. +15

voogos

what you are discussing is called cheating. Lie to your teachers, degrade the efforts of your honest colleagues who actually do the work, waste the chance to learn for yourself, deceive uourself and others about your abilities...
What do you do in your spare time? Rob grannies?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.