Fall0ut
Fall0ut

Reputation: 71

UpdateByValue Function is not doing anything?

#include <iostream>
#include <string>
using namespace std;

class Person{
public:
    string name;
    int age, height, weight;

    Person(string name = "empty", int age = 0, int height = 0, int weight = 0) {
        this->name = name;
        this->age = age;
        this->height = height;
        this->weight = weight;
    }
    Person operator = (const Person &P) {
        name = P.name;
        age = P.age;
        height = P.height;
        weight = P.weight;

        return *this;
    }

    void setAge(int a){
        age = a;
    }
    int getAge(){
        return age;
    }

    friend ostream& operator<<(ostream& os, const Person& p);
};

ostream& operator<<(ostream& os, Person& p) {
    os << "Name: " << p.name << "    " << "Age: " << p.age << "       " << "Height: " << p.height << "    " << "Weight: " << p.weight << "\n";
    return os;
};

class Node {
public:
    Person* data;
    Node* next;
    Node(Person*A) {
        data = A;
        next = nullptr;
    }
};

class LinkedList {
public:
    Node * head;
    LinkedList() {
        head = nullptr;
    }

    void InsertAtHead(Person*A) {
        Node* node = new Node(A);
        node->next = head;
        head = node;
    }
    void InsertAtEnd(Person*A) {
        if (head == nullptr) {
            InsertAtHead(A);
        }
        else {
            Node* node = new Node(A);
            Node* temp = head;
            while (temp->next != nullptr) {
                temp = temp->next;
            }
            temp->next = node;
        }
    }
    void InsertAtPosition(Person*A, int pos) {
        if (head == nullptr) {
            InsertAtHead(A);
        }
        else {
            Node* node = new Node(A);
            Node* temp = head;
            for (int i = 1; i < pos - 1; i++) { temp = temp->next; }
            node->next = temp->next;
            temp->next = node;
        }
    }
    void DeleteByValue(string search_name) {
        Node* temp = head;
        Node* prev = nullptr;
        while (temp != nullptr) {
            if (temp->data->name == search_name) {
                if (prev != nullptr) {
                    prev->next = temp->next;
                }
                else {
                    head = temp->next;
                }
                delete temp;
                temp = nullptr;
            }
            else {
                prev = temp;
                temp = temp->next;
            }
        }
    }
    void DeleteFromHead() {
        if (head != nullptr) {
            Node* temp = head;
            head = head->next;
            delete temp;
        }
    }
    void DeleteFromEnd() {
        Node* prev = nullptr;
        Node* temp = head;
        if (head == nullptr) { cout << "Nothing to delete" << endl; }
        else if (head->next == nullptr) { DeleteFromHead(); }
        else {
            while (temp->next != nullptr) {
                prev = temp;
                temp = temp->next;
            }
            prev->next = nullptr;
            delete temp;
        }
    }
    void DeleteAtPosition(int pos) {
        Node* prev = nullptr;
        Node* temp = head;
        if (head == nullptr) { cout << "Nothing to delete" << endl; }
        else if (pos == 1) { DeleteFromHead(); }
        else {
            for (int i = 1; i < pos; i++) {
                prev = temp;
                temp = temp->next;
            }
            prev->next = temp->next;
            delete temp;
        }
    }
    void UpdateAtPosition(Person*A, int pos) {
        if (head == nullptr) { cout << "No element in the list"; return; }
        if (pos == 1) { head->data = A; }
        else {
            Node* temp = head;
            for (int i = 1; i < pos; i++) {
                temp = temp->next;
            }
            temp->data = A;
        }
    }
    void UpdateByValue(string name, int newAge) {
        Node* temp = head;
        Person* p = new Person();

        while(temp != nullptr){
            if(temp->data->name == name){
                p->setAge(newAge);
            }else{
                temp = temp->next;
            }
        }
    }

    void Print() {
        Node* temp = head;
        while (temp != nullptr) {
            cout << *(temp->data);
            temp = temp->next;
        }
        cout << endl;
    }
};
int main() {
    LinkedList* list = new LinkedList();
    list->InsertAtHead(new Person("Samantha", 20, 63, 115));                  list->Print();
    list->InsertAtEnd(new Person("Chris", 19, 70, 200));                      list->Print();
    list->DeleteByValue("Chris");                                             list->Print();
    list->UpdateByValue("Samantha", 21);                                      list->Print();

    return 0;
}

I am new to C++ so excuse any poorly written code, but I am trying to use the function UpdateByValue to update the age of Samantha. It may look very wrong right now, but I have tried 20 different things and cannot figure out what I am doing wrong. I used to go to school at a community college where I learned Java so Im catching up to everyone in C++. A lot of it is similar but I struggle with little things like this. Could anyone explain to me how to fix the UpdateByValue function so that it will change the age of my Person object of choice? I want to be able to type the name as the first parameter and change the age of that person with the second parameter. If something is unclear and needs more explaining please let me know, I just need help. Thanks in advance, and please feel free to give any other constructive criticism. I am trying to get as good as I can.

Upvotes: 0

Views: 39

Answers (1)

user4581301
user4581301

Reputation: 33931

Let's take a walk through UpdateByValue. I'll comment as we go.

void UpdateByValue(string name, int newAge) {
    Node* temp = head;
    Person* p = new Person();

    while(temp != nullptr){ // keep looking until end of list
        if(temp->data->name == name){ // found node with name
            p->setAge(newAge); // update a different node
            // never advance node so we can't exit function
        }else{
            temp = temp->next;
        }
    }
}

Try instead

void UpdateByValue(string name, int newAge) {
    Node* temp = head;
    // Person * p   is not needed 

    while(temp != nullptr){ // keep looking until end of list
        if(temp->data->name == name){ // found node with name
            temp->data->setAge(newAge); // update the found node
            return; // done searching. Exit function
        }else{
            temp = temp->next;
        }
    }
}

Upvotes: 2

Related Questions