I'm having trouble with the 3rd if statement of my code if(select == 3). Here, if the user selects '3', then they should be able to search for students (given that students have already been inputted), via their ID. The ID that is searched and the subsequent Name relating to that ID should also be output. However, my code isn't doing that, and it can only search for the first student. Once it looks for the second student (and so on), it does nothing.

#include <iostream>
#include <cstdlib>

using namespace std;

int counter = 1;

struct Node
{
    char name[100];
    int age;
    int ID;
    Node *link;
};

int main()
{
    Node *start = NULL;
    int select = 1;
    while(select)
    {
        cout << "1. " << "List All Values in the List\n";
        cout << "2. " << "Add a New Value at the End of the List\n";
        cout << "3. " << "Search via ID\n";
        cout << "0. " << "Exit the Program\n";
        
        cin >> select;
        
        Node *temp, *temp2;
        
        if(select == 1)
        {
            //Node *temp;
            temp = start;
            system("cls");
            cout << "ID\tName\tAge\n";
            while(temp != NULL)
            {
                cout << temp -> ID << "\t" << temp -> name << "\t" << temp -> age << endl;
                temp = temp -> link;
            }
        }
        if(select == 2)
        {
            //Node *temp, *temp2;
            temp = new Node;
            cout << "Enter Name:\n";
            cin >> temp -> name;
            cout << "Enter Age:\n";
            cin >> temp -> age;
            temp -> link = NULL;
            if(start == NULL)
            {
                start = temp; //gives start the address of the new node
            }
            else
            {
                temp2 = start;//gives the new node the address of start
                while(temp2 -> link != NULL)//while the address of temp2 is not NULL
                {
                    temp2 = temp2 -> link;
                }
                temp2 -> link = temp;
            }
            temp -> ID = counter;
            counter++;
            system("cls");
        }
        
    	if(select == 3)//having trouble here, it doesn't ouput the ID and names of the students as it should
    	{
    		temp2 = start;    		
    		cout << "Enter ID of Person of Interest: ";
    		int search;
    		cin >> search;
    		
    		bool decision=1;//as of true by default
    		
            while(temp2 -> link != NULL)
            {
                  if(search == temp2 -> ID)
            		{
            			cout << "ID\tName\n";
            		    cout << temp2 -> ID << "\t" << temp2 -> name << endl;
                        decision = 0;
                    }    
            }
    		if(decision)
    		{
                cout << "record not found\n";    			
    		}
    	}
    }
    return 0;
}

Any help is truly appreciated :)

Recommended Answers

All 2 Replies

You have two issues. First, the condition should be while (temp2 != NULL) because you want to include the last node in the search. Second, your search loop doesn't update temp2; the loop is infinite. Compare and contrast:

while(temp2 != NULL) {
    if(search == temp2 -> ID) {
        cout << "ID\tName\n";
        cout << temp2 -> ID << "\t" << temp2 -> name << endl;
        decision = 0;
    }
    
    temp2 = temp2 -> link;
}

Thanks so much :D I was able to fix up the while loop but then I didn't pick up the last part.

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.