1

I'm a coding newbie and I've been lost to what my program is doing. Basically, my add() method abruptly stops when I call it by case 1, 3 times, and then it terminates the program when I want to add more student ID to my node. I'm at a loss and I have been trying to find out what's wrong. Any help would be appreciated!

#include <iostream>
using namespace std;

struct Node
{
    int ID;
    Node *next;
}; Node *head = NULL;

void Menu();
void Add();
void Delete();
void Search();
void Display();

int main()
{
    int Options = 0;
        cout << "Welcome to Student Records. Choose an option:\n"
             << "1.) Add student\n"
             << "2.) Delete student\n"
             << "3.) Search student\n"
             << "4.) Display list of students\n"
             << "5.) Exit\n\n";
        cout << "Input: ";
        cin >> Options;

        do{
        switch(Options)
        {
        case 1:
            Add();
            break;
        case 2:
            Delete();
            break;
        case 3:
            Search();
            break;
        case 4:
            Display();
            break;
        default:
            cout << "Operation terminated.";
        }

        cout << "\nWhat's next?\n";
        Menu();
        cin >> Options;
        }while(Options == 1 || Options == 2 || Options == 3 || Options == 4);

        cout << "Exit program?";

    return 0;
}//end main

void Menu()
{
        cout << "1.) Add student\n"
             << "2.) Delete student\n"
             << "3.) Search student\n"
             << "4.) Display list of students\n"
             << "5.) Exit\n\n";
}

void Add()
{
    int id;
    Node *node_new;
    Node *pointer;
    Node *lastNode = NULL;

    node_new = new Node;

    cout << "Enter 3-digit Student ID#: ";
    cin >> id;
    node_new -> ID = id;

    if (head == NULL) //if list is empty
    {
        head = node_new;
        node_new -> next = NULL;
    }

    else //if head is not NULL
    {
        pointer = head;
        lastNode = NULL;

        while(pointer -> ID < id && pointer != NULL)
        {
            lastNode = pointer;
            pointer = pointer -> next;
        }

        if (lastNode == NULL)
        {
            head = node_new;
            node_new -> next = pointer;
        }

        else
        {
            lastNode -> next = node_new;
            node_new -> next = pointer;
        }
    }
}

void Delete()
{
    int id;
    Node *pointer;
    Node *lastNode;

    cout << "Enter Student ID# you wish to delete: ";
    cin >> id;

    if (head == NULL)
    {
        cout << "The list is already empty.";
    }
    else if (head -> ID == id)
    {
        pointer = head;
        head = head -> next;
        delete pointer;
    }
    else
    {
        pointer = head;

        while(pointer -> ID != id && pointer != NULL)
        {
            lastNode = pointer;
            pointer = pointer -> next;
        }


        if(pointer == NULL)
            cout << "Student does not exist.";
        else
            lastNode -> next = pointer -> next;
            delete pointer;
    }
}

void Display()
{
    Node *pointer;

    pointer = head;

    do{
        if(pointer == NULL)
            cout << "The list is already empty!";
        else
        {
            cout << "Student ID# " << pointer -> ID << endl;
            pointer = pointer -> next;
        }
    }while(pointer != NULL);
}

void Search()
{
 int id;
 Node *pointer;

 pointer = head;

 cout << "Who do you want to search? Enter ID#: ";
 cin >> id;

do{

    if(pointer == NULL)
        cout << "Empty";
    else
    {
         cout << "ID: " << pointer -> ID << endl;
         pointer = pointer -> next;
    }


}while(pointer != NULL);

}
caabaeg
  • 11
  • 1
  • Did you try stepping through your code, **line-by-line**, with a debugger, while investigating the values of variables, at **each** execution step, to notice, when the values of those, stop matching your expectations? – Algirdas Preidžius Dec 13 '19 at 19:10
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl Dec 13 '19 at 19:20
  • 3
    Consider swapping the order of these: `pointer -> ID < id && pointer != NULL` – AndyG Dec 13 '19 at 19:30
  • Groovy. Code compiles now, so with the correct inputs I suppose the bug is now reproducible. What are the correct inputs? Better yet, back up your code and hack out all the pieces of the program that you DON'T need in order to reproduce the bug. This will leave you with a nice, tight little program that anyone can run to quickly examine the error. The true beauty of doing this is you'll probably find the and fix bug in the process. Use [mcve] as inspiration. – user4581301 Dec 13 '19 at 20:30

0 Answers0