This information is very useful but not exactly what I need to do. I want it to search through the entire list, find the one I'm looking for, ask the user what values they want to change and then change it accordingly

Edited 5 Years Ago by Cross213: n/a

This is the first Linked List I have ever written but is this what you are trying to do?

The Change() function searches the list for an entry with the same name and then asks to change the age of the person.

#include <iostream>
using namespace std;

struct Node
{
	Node(){};
	Node( string n, int a )
	{
		name = n;
		age = a;
		next = NULL;
	}
	string name;
	int age;
	Node *next;
};

class List
{
	Node *first;

	public:

	List()
	{
		first = NULL;
	}

	void Add(string name, int age) //adds a new entry to the list
	{
		Node *newNode = new Node(name, age);
		Node *curNode = first;


		if( curNode == NULL )
		{
			first = newNode;
			return;
		}

		while(curNode->next != NULL)
		{
			curNode = curNode->next;
		}
		curNode->next = newNode;
	}

	void Change(string name) //finds and changes the 
	{
		Node *curNode = first;
		bool found = false;

		if( curNode == NULL )
			return;

		if( curNode->name == name )
			found = true;

		while(curNode->next != NULL || found != true)
		{
			curNode = curNode->next;
			if( curNode->name == name )
				found = true;
		}

		if( found == false )
			return;

		cout << "What would you like " << curNode->name << "'s new age to be? (currently " << curNode->age << ")" << endl;
		cin >> curNode->age;
		cout << endl;

	}

	void Print() //displays contents of the list
	{
		Node *curNode = first;

		if( curNode == NULL )
			return;
		cout << curNode->name << " " << curNode->age << endl;
		while(curNode->next != NULL)
		{
			curNode = curNode->next;
			cout << curNode->name << " " << curNode->age << endl;
		}
		cout << endl;
	}
};


int main()
{
	List people;
	people.Add("Jim", 22);
	people.Add("Jane", 18);
	people.Print();
	people.Change("Jane");
	people.Print();
    return 0;
}

I'm sure the functions I wrote could be cleaned up but I was just aiming to get it working for now.

This article has been dead for over six months. Start a new discussion instead.