I'm working on a lab for my C++ class that is the first to use classes and objects. It requires a search function that lists tasks found for a target course. I can't get the search function to work. Here is the relevant code:

from main source code:

void processCommand(char command, TaskList& list)
{
	Task			entry;
	char			task[MAX_CHAR];
	char			course[MAX_CHAR];
	char			date[MAX_CHAR];

	switch (command)..
case 's': readInTask(course);
			  if(list.searchEntry(course, entry))
			  {
				   entry.getTask(task);   // gets matching task 
				   entry.getDate(date);	  // gets matching date
				   cout << endl << " The tasks for course " << course << " are: \n" << endl;
				   //column titles
				   cout << setw(TASK_COL_WIDTH) << "  Task" 
						<< setw(DATE_COL_WIDTH) << "  Date";
				   cout << endl ;

				   cout << setw(TASK_COL_WIDTH) << task 
					<< setw(DATE_COL_WIDTH) << date
					<< endl;
			  }

.. [B]from TaskList::searchEntry function[/B]
bool TaskList::searchEntry(const char course[], Task& match) const
{
	int		index;
	char	currentTask[MAX_CHAR];
	char	currentCourse[MAX_CHAR];
	char    currentDate[MAX_CHAR];

	for(index=0; index < size; index++)
	{
		list[index].getTask(currentTask);
		list[index].getCourse(currentCourse);
		list[index].getDate(currentDate);
		if(strcmp(course, currentCourse) == 0)
		{
			match.setTask(currentTask);
			match.setCourse(currentCourse);
			match.setDate(currentDate);
		}
	}
	if (index == size)
		return false;
	else
		return true;
}

The search outputs one search result when there are more than one. I can't tell if the problem is in the output (in the switch statement) or in the actual search function.

Thanks for your help.
WFWS

I only see a single output for when something matches:

entry.getTask(task);   // gets matching task 
entry.getDate(date);	  // gets matching date

Without exactly know how your class Task looks like, I can't help any further. What is the output supposed to look like? What is the current output?

Here are the definitions from the implementation file for the Task class. The search output is supposed to list all tasks found for a particular course, and their date. So the output would be columns under the task and date headers. The output I'm getting now is either nothing found or a single task when it should list several. If you need the task.h file let me know.

#include "Task.h"
#include <iostream>
using namespace std;

/* Constructors*/
Task::Task()
{
	strcpy(task, "no task");
	strcpy(course, "no course");
}
/* Constructor initializes Task thru parameters*/
Task::Task(const char task[], const char course[], const char date[])
{
	strcpy(this->task, task);	
	strcpy(this->course, course); 
	strcpy(this->date, date);
}

/* getTask returns the name of the task through the parameter.
   out: task
*/
void Task::getTask(char task[]) const
{
	strcpy(task, this->task);
}

/* getCourse function returns the course of the entry through the parameter.
   out: course
*/
void Task::getCourse(char course[]) const
{
	strcpy(course, this->course);
}

/* getDate function returns the date of the entry through the parameter.
   out: date
*/
void Task::getDate(char date[]) const
{
	strcpy(date, this->date);
}

/*Print function prints the state of the object.*/
void Task::print() const
{
	cout << task << " -> " << course << endl;
}

/* setTask sets the task to the passed in value.
   in: task
*/
void Task::setTask(const char task[])
{
	strcpy(this->task, task);
}

/* setCourse sets the course to the passed in value.
   in: course
*/
void Task::setCourse(const char course[])
{
	strcpy(this->course, course);
}

/* setDate sets the course to the passed in value.
   in: course
*/
void Task::setDate(const char date[])
{
	strcpy(this->date, date);
}

Alright. The issue is in both of your searching and processing methods.

Problem with your search method: If you are expecting one or more results, but only accept one reference Task object, then you cannot expect to process multiple results.

Problem with your process method: You will need to update it to loop through all possible results.

Now for an observation: it is quite evident that you are using C++, yet you decided to use C-type strings (char arrays). Any particular reason for that?

Thanks- so I'm understanding this- I need to correct both the output and the search functions to allow multiple results.

We are restricted to using only c-strings for this class by the instructor.

I think I can understand using a loop to go thru possible results. I'm not sure I get how to tell search to accept more than one task object. Can you give an example? Will it require a loop as well?

Your answer is VERY helpful. I wasn't sure what side the problem was on- it appears its on BOTH search and output.

What you could do is pass a reference to a vector of Task objects, like so:

bool TaskList::searchEntry(const char course[], vector<Task>& matches) const

Then, each time a match is found, just add a task to it.

When looping through your search results, just iterate through the vector.

Edited 5 Years Ago by MonsieurPointer: n/a

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