Hello,
My program runs and I have successfully written my Find and Display functions for my vector and have gotten them to work. However, my Remove and Search functions are not working; to be more specific, they are able to search for a value, but no value is found. Any help would be appreciated! Thanks in advance.

BTW, if my code is hard to read, I can post the .cpp for you guys.

Here is my code so far:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class InventoryInfo  
{
        string InventoryID;
        string InventoryDesc;
        double InventoryCost;

public:
		InventoryInfo() {InventoryID=""; InventoryDesc=""; InventoryCost=0;}
        InventoryInfo(string ID, string Desc, double Cost)
		{
			InventoryID=ID;
			InventoryDesc=Desc;
			InventoryCost=Cost;
		}
 
		void SetID(string ID) {InventoryID=ID;} 
		void SetDesc(string Desc) {InventoryDesc=Desc;}
		void SetCost(double Cost) {InventoryCost=Cost;}

		string GetID() {return InventoryID;}
		string GetDesc() {return InventoryDesc;} 
		double GetCost() {return InventoryCost;}
};

void Fill(vector<InventoryInfo>& iInfo);
void Show(vector<InventoryInfo>);
void Find(vector<InventoryInfo> iInfo);
void Remove(vector<InventoryInfo>& iInfo);

int main()
{
        vector<InventoryInfo> iInfo;  //Creates an object...vector of strings...

		Fill(iInfo);
        Show(iInfo);
		Remove(iInfo);
		Show(iInfo);
        Find(iInfo);
}

void Fill(vector<InventoryInfo>& iInfo)
{
		cout<<"*** Add item ***"<<endl;

		string ID, Desc;
		double Cost;

		cout<<"Please enter an item ID, type 'stop' to cancel"<<endl;
		cout<<"> ";
		getline(cin, ID);
		if(ID=="stop")
			return;

		cout<<"Please give a description of the item"<<endl;
		cout<<"> ";
		getline(cin, Desc);

		cout<<"What is the cost of the item? (numbers and decimals only, no commas)"<<endl;
		cout<<"> ";
		cin>>Cost;

		InventoryInfo Value0;
		Value0.SetID(ID);
		Value0.SetDesc(Desc);
		Value0.SetCost(Cost);

		iInfo.push_back(Value0);
}

void Show(vector<InventoryInfo> iInfo) // reference!
{
	vector<InventoryInfo>::iterator AccessInventory; //Sets up an iterator...

	for( AccessInventory = iInfo.begin() ; AccessInventory != iInfo.end() ; ++AccessInventory)
	{
	cout<<"\n> Item ID: "<<AccessInventory->GetID() << endl; // use iterator...
	cout<<"> Item description: "<<AccessInventory->GetDesc() << endl;
	cout<<"> Item cost: $"<<AccessInventory->GetCost() << endl;
	}
}

void Find(vector<InventoryInfo> iInfo)
{
	string Search;

	cout<<"\nPlease enter item ID to search: ";
	getline(cin, Search);
	
	vector<InventoryInfo>::iterator AccessInventory; //Sets up an iterator...

	for( AccessInventory = iInfo.begin() ; AccessInventory != iInfo.end() ; ++AccessInventory)
	{
		if(AccessInventory->GetID()==Search)
		{	
			cout<<"\n> Item ID: "<<AccessInventory->GetID() << endl; // use iterator...
			cout<<"> Item description: "<<AccessInventory->GetDesc() << endl;
			cout<<"> Item cost: $"<<AccessInventory->GetCost() << endl;
			break;
		}
		else
		cout<<"\nItem ID not found, please try again..."<<endl;
	}
}

void Remove(vector<InventoryInfo>& iInfo)
{
        string Remove;

		cout<<"\nPlease enter item ID you wish to delete: "<<endl;
		getline(cin, Remove);
	
		vector<InventoryInfo>::iterator AccessInventory;
 
        for(AccessInventory = iInfo.begin() ; AccessInventory != iInfo.end() ; ++AccessInventory)
                if(AccessInventory->GetID() == Remove)  
				{
                        iInfo.erase(AccessInventory);
                        return;
                }
        cout<<"\nItem ID not found, please try again..."<<endl;
}

Your code works fine for me, except that you do not flush the input buffer, so the Remove function doesn't wait for my input on the getline call, it straight passes the getline (because there is 'garbage' in the input stream). After that the search returns my added item correctly.

Learn how to flush input streams here:
http://www.daniweb.com/forums/thread90228.html

Member Avatar for jencas
void Find(vector<InventoryInfo> iInfo)
{
	string Search;

	cout<<"\nPlease enter item ID to search: ";
	getline(cin, Search);
	
	vector<InventoryInfo>::iterator AccessInventory; //Sets up an iterator...

	for( AccessInventory = iInfo.begin() ; AccessInventory != iInfo.end() ; ++AccessInventory)
	{
		if(AccessInventory->GetID()==Search)
		{	
			cout<<"\n> Item ID: "<<AccessInventory->GetID() << endl; // use iterator...
			cout<<"> Item description: "<<AccessInventory->GetDesc() << endl;
			cout<<"> Item cost: $"<<AccessInventory->GetCost() << endl;
			break;
		}
		else
		cout<<"\nItem ID not found, please try again..."<<endl;
	}
}

...outputs "...not found..." for every element of the vector which does not equal the search argument. Very bad behaviour!

This code is NOT fine. The problem is the there is not a copy constructor or an assignment operator. The requirement for using vectors SAFELY is that they work.

If you do not provide them, then the compiler uses a default version. Your class requires that you has std::string which ARE NOT simple objects. Therefore, when the default copy constructor runs which does a simple memory copy, you have trailing memory references. Therefore if you do things like remove an element and add a new element, or sort the vector all kinds of unexpected junk happens.

The effect of this is that this code is 100% unpredicatable.

Rules:

ALWAY provide a copy constructor and an assignment operator for ALL classes that have anything other than simple memory data. ie. all pointers, all complex classes. If in doubt provide one.

If you really really don't think you use one, then put a private definition.

Sorry for the rant. I wish the compilers gave much stronger warnings on this.

Additionally, please use some const, get functons are const, references that are only accessed are const etc.

Finally, do not use using namespace std; , that will get you into trouble later. you will acidentally overwrite string/vector etc. and not have the slightest clue what is going on.
It is slightly more typing but saves huge effort in debugging later.

Otherwize a worthy effort at your problem!

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.