Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to search tutors by overall performance or rating. Which means it will return multiple values. But I only got 1 value. How do I get the code to return multiple tutors instead of only 1, even though their are multiple tutors with the same rating.

What I have tried:

Search Function:
C++
cout << "Enter Rating:" << endl;
		cin >> rating;
		while (current != NULL)
		{
			if (current->rating == rating) 
			{
				cout << "Tutor ID:" << current->tutorID << endl;
				cout << "Tutor Name:" << current->tutorName << endl;
				cout << "Date Joined:" << current->day_join << "/" << current->month_join << "/" << current->year_join << endl;
				cout << "Date Terminated:" << current->day_ter << "/" << current->month_ter << "/" << current->year_ter << endl;
				cout << "Hourly Pay Rate:" << current->hourly_pay_rate << endl;
				cout << "Phone:" << current->phone << endl;
				cout << "Address:" << current->address << endl;
				cout << "Center Code:" << current->centerCode << endl;
				cout << "Center Name:" << current->centerName << endl;
				cout << "Subject Code:" << current->subjectCode << endl;
				cout << "Subject Name:" << current->subjectName << endl;
				cout << "Rating:" << current->rating << endl;


				flag = 1; /*tutor details found*/
				break;
			}
			current = current->next; /*move to one node to another*/

		}
		if (flag == 0) /*if still no match found*/
			cout << "Not found" << endl;
struct Node 
{
	int tutorID;
	string tutorName;
	int day_join;
	int month_join;
	int year_join;
	int day_ter;
	int month_ter;
	int year_ter;
	int hourly_pay_rate;
	int phone;
	string address;
	int centerCode;
	string centerName;
	int subjectCode;
	string subjectName;
	int rating;
	struct Node* prev;
	struct Node* next;
};
Posted
Updated 20-Mar-20 22:55pm
v2

You have:
C++
while(current != NULL)
{
    if (current->rating == rating)
    {
        // display details ...
        
        flag = 1;
        break;    // <== !!!
     }
     // etc
}

The break in the while loop terminates the loop, so you only get the first matching item. You probably want to remove that.

Why not use an STL container e.g. std::list or a std::vector, then use std::copy_if to create a new container with the matching items in it?
 
Share this answer
 
You will have to create a new collection containing pointers to "found nodes",.
You cannot modify the node "next" or "previous" links, or you will break the existing linked list.

Your collection could be an array, or a list of a new struct that contains the node pointer, and a "next" pointer so it can be iterated.
 
Share this answer
 
Please acquaint yourself with the standard library algorithms. It handles many academic and common real life items such as yours
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900