I need to create a search function that search for a product type. If it's found, I return true, otherwise i return false. My client program will output the data that was found (if true), otherwise it'll output an error message saying that product type does not exist in the data base.

Teachers notes:
* you might need to return more than one vendor
* you can make the client program to pass in an array (adata) and populate this array in the retrieve function

I'm using a hash table.. So basically this function brings in the key (which is the product type) from the users input. And we are passing in our data as well as a pointer.

Now, for some reason when I run my program, and I search for an item with only one unique product type, I don't get an error. But as soon as a search for a product type that many items have, then I get an error. Someone pls help me out, this is do soon and I'm really lost.

bool BST::retrieve(char * key, data * aData)const
{
	//calculate the retrieval position using hash function (getIndex)
	int i = getIndex(key); //Hash Function
	int j = 0;

	//search for the data in the chain (linked list)
	hashNode * curr = table[i];
	char productType[100];
	while(curr)
	{
		curr->item.getProductType(productType);
		if(strcmp(key, productType) == 0)
		{
			// Product type exists so populate array
			aData[j] = curr->item;
			curr = curr->next;
                        j++;
		}
		else
			curr = curr->next;
	}

	if(j > 0)
		return true;
	else
		return false;
}

Recommended Answers

All 9 Replies

no one can help, eh?

Where is the error exactly, cause at the moment I don't see any issue with the code. One thing to help determine if there is an issue is to add a cout line inside the if else in the while and see what it is doing.

Question, are the two items you are passing to this function pointers?

Where is the error exactly, cause at the moment I don't see any issue with the code. One thing to help determine if there is an issue is to add a cout line inside the if else in the while and see what it is doing.

Question, are the two items you are passing to this function pointers?

We are passing in a pointer (key) and an array (vendors).

So if I search for something with the same similar product, it goes through the first time and outputs it fine. But the second time we get through (marked by the error comment) we get an error. If I output someone right before, aData[j] = curr->item, it outputs fine. But anything I try to output after that doesn't get outputted because the program has already hung up.

if(strcmp(key, productType) == 0)
		{

			// Product type exists so populate array
			aData[j] = curr->item; //[B]ERROR OCCURS HERE[/B]
                        cout << aData[j] << endl;
			curr = curr->next;
                        j++;
		}

With what you have posted, the only thing I would say is that aData is too small, thus resulting in an error. If you want a more in depth answer, I'd have to see the rest of the code.

Just random, what are you using a char array instead of a string for productType?

With what you have posted, the only thing I would say is that aData is too small, thus resulting in an error. If you want a more in depth answer, I'd have to see the rest of the code.

Just random, what are you using a char array instead of a string for productType?

Because one of the requirements for this program is to use char * to model strings instead of string.

And I can give my entire code, but it's rather lengthy...

Header For BST (a collection class to manage the vendors you are interested.)

#ifndef BST_H
#define BST_H

#include "data.h"

class BST
{
public:
	BST();
	BST(char * fileName);
	BST(const BST& aBST);
	~BST();

	//const BST& operator= (const BST& aTable);

	void add(const data & aData);
	bool remove(char * name);
	//bool retrieve(char * name, data&)const;
	bool retrieve(char * key, data * aData)const;
	void displayByName()const;
	void displayByProductType()const;
	void writeOut(char * fileName)const;

private:
	struct treeNode
	{
		data item;
		treeNode * left, * right;
		treeNode(const data& aData)
		{
			item = aData;
			left = right = NULL;
		}
	};
	struct node 
    {
        data item;
        node * next;
		node(const data& aData)
		{
			item = aData;
			next = NULL;
		}
    }; 
    node * head;
	treeNode * root;	
	int size;

	// Link Functions
	void addToList(data * ptrItem);
	bool removeFromList(char * key);

	// Tree Functions
	void addToTree(data * ptrItem);
	void insert(treeNode*& root, data * ptrItem);
	//bool removeFromTree(char * key);
	void display(treeNode * root)const;
	void BST::destroyTree(treeNode *& root);
};
#endif

Header for data (a vendor class to model the vendor object).

#ifndef DATA_H
#define DATA_H
#include <iostream>

using namespace std;

class data
{
public:
	friend ostream& operator<< (ostream& outfile, const data& aData);
	data();
	data(char * vendorName, char * phoneNum, char * productType, char * events);
	data(const data& aData);		//copy constructor: make current object a copy of "data"
	~data();						//destructor: release the dynamically allocated memory

	const data& operator=(const data& student);	//overloading assignment operator

	void getVendorName(char * vendorName)const;
	void getPhoneNum(char * phoneNum)const;
	void getProductType(char * productType)const;
	int getEventNum(void)const;
	void getEvents(char * events)const;

	void setVendorName(char * vendorName);
	void setPhoneNum(char * phoneNum);
	void setProductType(char * productType);
	void setEventNum(int eventNum);
	void setEvents(char * events);
	int compareItem(data& d1, data& d2);

private:
	char * vendorName;
	char * phoneNum;
	char * productType;
	int	 eventNum;
	char * events;
};

bool operator< (const data& d1, const data& d2);
bool operator== (const data& d1, const data& d2);

#endif

MAIN

#include <stdlib.h>
#include <crtdbg.h>
#include "data.h"
#include "bst.h"

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cassert>

using namespace std;

void displayMenu();
char getCommand();
void executeCmd(char command, BST& vendors);
void getVendor(data & adata);
int getInt(char * prompt);
float getFloat(char * prompt);
void getString(char * prompt, char * input);
void displayTree(const BST & vendors);
void displayList(const BST & vendors);

const int MAX_LEN = 100;

int main() 
{
    char command = 'a';
	char inputFileName[] = "vendor-input.txt";
    char outputFileName[] = "vendor-output.txt";
    BST vendors(inputFileName);

	cout << "\nWelcome to CS 260 Vendor Management System! "<< endl;
	cout << "\nImplemented by: Jonah Brasseur" << endl;
	cout << "--------------------------------" << endl;
	    
    displayMenu();
    command = getCommand();

    while(command != 'q') 
	{
        executeCmd(command, vendors);
        displayMenu();
        command = getCommand();
    }
    
    vendors.writeOut(outputFileName);
    cout << "\nThank you ... exiting" << endl << endl;
    return 0;
}

void displayMenu() 
{
	cout << "Vendor Management System -- select a command:"<< endl;
    cout << "\ta: add a vendor" << endl
         << "\tr: remove a vendor (by specifying the name)" << endl
         << "\ts: search for a vendor (by providing the name)" << endl
         << "\tp: search for a vendor (by providing product type)" << endl 
		 << "\td: display vendors (sorted by name)" << endl 
		 << "\tv: display vendors (sorted by product type)" << endl 
		 << "\tt: display internal tree diagram" << endl 
		 << "\tq: quit and save" << endl 
         << "\tx: exit without saving" << endl << endl;
}

char getCommand() 
{
    char cmd;
    cout << "Please enter your choice:";
    cin >> cmd;
    cin.ignore(100, '\n');
    return tolower(cmd);
}

void executeCmd(char command, BST & vendors) 
{
    data adata;
    char key[MAX_LEN];
	int i = 0;

    switch(command) 
	{
        case 'a': getVendor(adata);
				  vendors.add(adata);
				  cout << "added to the database" << endl << endl;
                  break;
        case 'r': getString("\nPlease enter the name of the winery you want to remove: ", key);
                  if(vendors.remove(key))
					  cout << endl << key << " has been removed from the database" << endl << endl;
				  else
					  cout << endl << key << " does not exist in the database" << endl << endl;
                  break;
        case 's': getString("\nPlease enter the name of the winery you want to search: ", key);
                  //aList.retrieve(key, adata);
                  break;
	    case 'p': getString("\nPlease enter the name of the winery you want to search: ", key);
				  vendors.retrieve(key, &adata);
				  //	 cout << endl << "Information about " << key << ": " << endl << endl /*<< adata*/ << endl << endl;
                  //else
                  //    cout << endl << "Information about " << key << ": " << '\t' << "Not found" << endl << endl;
                  break;
        case 'd': displayTree(vendors);
                  break;
        case 'v': displayList(vendors);
                  break;
        case 't': //display(vendors);
                  break;
		case 'x': //abort();
                  break;
        default:  cout << "illegal command!" << endl;
                  break;
    }
}

void getVendor(data & adata) 
{
    char vendorName[MAX_LEN];
    char phoneNum[MAX_LEN];
    char productType[MAX_LEN];
    int eventNum;
    char events[MAX_LEN];

    cout << "\nPlease enter information about the winery (type q to quit): " << endl;
    getString("\tVendor name: ", vendorName);
	if ((vendorName != "q") || (vendorName != "Q"))
	{
    getString("\tPhone number: ", phoneNum);
    getString("\tType of product: ", productType);
    eventNum = getInt("\tThe amount of events: ");
    getString("\tName(s) of events: ", events);

    adata.setVendorName(vendorName);
    adata.setPhoneNum(phoneNum);
    adata.setProductType(productType);
    adata.setEventNum(eventNum);
    adata.setEvents(events);
	}
}

int getInt(char * prompt) 
{
    int temp;
    cout << prompt;
    cin >> temp;
    while(!cin) {
        cin.clear();
        cin.ignore(100, '\n');
        cout << "Illegal input -- try again: ";
        cin >> temp;
    }
    cin.ignore(100, '\n');
    return temp;
}

float getFloat(char * prompt) 
{
    float temp;
    cout << prompt;
    cin >> temp;
    while(!cin) {
       cin.clear();
       cin.ignore(100, '\n');
       cout << "Illegal input -- try again: ";
       cin >> temp;
    }
    cin.ignore(100, '\n');
    return temp;
}
void getString(char * prompt, char * input) 
{
    cout << prompt;
    cin.get(input, MAX_LEN, '\n');
    cin.ignore(100, '\n');
}
void displayTree(const BST & vendors) 
{
    vendors.displayByName();
}
void displayList(const BST & vendors) 
{
    vendors.displayByProductType();
}

My assumption would be it's because aData is simply one thing.

void executeCmd(char command, BST & vendors) {
    data adata;
    ...
    //aList.retrieve(key, adata);

You're trying to save it like an array, but twhat you have it initialized as is a single data, not a collection. Just add another variable like data arrdata[50]; or something and pass it and see if that clears up the issue.

My assumption would be it's because aData is simply one thing.
void executeCmd(char command, BST & vendors) {
data adata;
...
//aList.retrieve(key, adata);

You're trying to save it like an array, but twhat you have it initialized as is a single data, not a collection. Just add another variable like data arrdata[50]; or something and pass it and see if that clears up the issue.

I'm retarded, and you are a life saver.

I hate how I can get stuck on a simple problem for hours, thanks though.

No problem. Glad I could be of help. And believe me, you aren't the only one who gets stuck on simple problems.

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.