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;
}