Thanks again anon! I worked long and hard on this program and it finally runs ^^. I've added a menu() and 2 more strings in the class. Everything runs fine, but when I display or search, the values are all weird. They were fine before. Do you know what's wrong with it?
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class InventoryInfo
{
string InventoryID;
string InventoryDesc;
string InventoryCost;
string ItemLocation;
string ItemsInStock;
public:
InventoryInfo() {InventoryID=""; InventoryDesc=""; InventoryCost=""; ItemLocation=""; ItemsInStock="";}
InventoryInfo(string ID, string Desc, string Cost, string Loc, string Stock)
{
InventoryID=ID;
InventoryDesc=Desc;
InventoryCost=Cost;
ItemLocation=Loc;
ItemsInStock=Stock;
}
void SetID(string ID) {InventoryID=ID;}
void SetDesc(string Desc) {InventoryDesc=Desc;}
void SetCost(string Cost) {InventoryCost=Cost;}
void SetLocation(string Loc) {ItemLocation=Loc;}
void SetInStock(string Stock) {ItemsInStock=Stock;}
string GetID() {return InventoryID;}
string GetDesc() {return InventoryDesc;}
string GetCost() {return InventoryCost;}
string GetLocation() {return ItemLocation;}
string GetInStock() {return ItemsInStock;}
};
void Fill(vector<InventoryInfo>& iInfo);
void Show(vector<InventoryInfo> iInfo);
void Find(vector<InventoryInfo> iInfo);
void Remove(vector<InventoryInfo>& iInfo);
void WriteToFile(vector<InventoryInfo> iInfo);
void Menu();
int main()
{
string Command;
vector<InventoryInfo> iInfo; //Creates an object...vector of strings...
while(true){
Menu();
getline(cin, Command);
if (Command == "exit")
{
break;
}
else if (Command == "add")
{
Fill(iInfo);
cout << "\n" << endl;
}
else if (Command == "delete")
{
cout<<"\n"<<endl;
Remove(iInfo);
cout<<"\n"<<endl;
}
else if (Command == "display")
{
cout << "\n" << endl;
Show(iInfo);
cout << "\n" << endl;
}
else if (Command == "search")
{
cout<<"\n"<<endl;
Find(iInfo);
cout<<"\n"<<endl;
}
else if (Command == "save")
{
cout<<"\n"<<endl;
WriteToFile(iInfo);
cout<<"\n"<<endl;
}
else
{
cout << "Please try another command\n" << endl;
}
}
}
void Menu() //This is displayed on the menu
{
cout << "#################################################################\n" << endl;
cout << "Please choose one of the following commands:\n" << endl;
cout << "add" << endl;
cout << "delete" << endl;
cout << "display" << endl;
cout << "search" << endl;
cout << "save" <<endl;
cout << "exit"<< endl;
cout << "\n#################################################################\n" << endl;
}
void Fill(vector<InventoryInfo>& iInfo)
{
cout<<"\n*** Add item ***"<<endl;
string ID, Desc, Cost, Loc, Stock;
for(;;)
{
cout<<"\n=================================================================="<<endl;
cout<<"Please enter an item ID, type 'stop' to cancel"<<endl;
cout<<"> ";
getline(cin, ID);
if(ID=="stop")
break;
cout<<"\nPlease give a description of the item"<<endl;
cout<<"> ";
getline(cin, Desc);
cout<<"\nWhat is the cost of the item?"<<endl;
cout<<"> $";
getline(cin, Cost);
cout<<"\nPlease describe the location of the item"<<endl;
cout<<"> ";
getline(cin, Loc);
cout<<"\nHow many items are in stock?"<<endl;
cout<<"> ";
getline(cin, Stock);
cout<<"=================================================================="<<endl;
InventoryInfo Value0;
Value0.SetID(ID);
Value0.SetDesc(Desc);
Value0.SetCost(Cost);
Value0.SetCost(Loc);
Value0.SetCost(Stock);
iInfo.push_back(Value0);
}
}
void Show(vector<InventoryInfo> iInfo) // reference!
{
cout<<"\n*** Item Inventory ***"<<endl;
vector<InventoryInfo>::iterator AccessInventory; //Sets up an iterator...
for( AccessInventory = iInfo.begin() ; AccessInventory != iInfo.end() ; ++AccessInventory)
{
cout<<"\n=================================================================="<<endl;
cout<<"> Item ID: "<<AccessInventory->GetID() << endl; // use iterator...
cout<<"> Item description: "<<AccessInventory->GetDesc() << endl;
cout<<"> Item cost: $"<<AccessInventory->GetCost() << endl;
cout<<"> Item location: $"<<AccessInventory->GetLocation() << endl;
cout<<"> Items in stock: $"<<AccessInventory->GetInStock() << endl;
cout<<"=================================================================="<<endl;
}
}
void Find(vector<InventoryInfo> iInfo)
{
cout<<"\n*** Find Item ***"<<endl;
string Search;
cout<<"\nPlease enter item ID to search: "<<endl;
cout<<"> ";
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=================================================================="<<endl;
cout<<"> Item ID: "<<AccessInventory->GetID() << endl; // use iterator...
cout<<"> Item description: "<<AccessInventory->GetDesc() << endl;
cout<<"> Item cost: $"<<AccessInventory->GetCost() << endl;
cout<<"> Item location: $"<<AccessInventory->GetLocation() << endl;
cout<<"> Items in stock: $"<<AccessInventory->GetInStock() << endl;
cout<<"=================================================================="<<endl;
return;
}
}
cout<<"\nItem ID not found, please try again..."<<endl;
return;
}
void Remove(vector<InventoryInfo>& iInfo)
{
cout<<"\n*** Remove Item ***"<<endl;
string Remove;
cout<<"\nPlease enter item ID you wish to delete: "<<endl;
cout<<"> ";
getline(cin, Remove);
cout<<"\nItem inventory updated!"<<endl;
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;
return;
}
void WriteToFile(vector<InventoryInfo> iInfo)
{
fstream OutFile("FINAL.dat", ios::out);
vector<InventoryInfo>::iterator AccessInventory; //Sets up an iterator...
for( AccessInventory = iInfo.begin() ; AccessInventory != iInfo.end() ; ++AccessInventory)
{
OutFile<<AccessInventory->GetID()<<endl;
OutFile<<AccessInventory->GetDesc()<<endl;
OutFile<<AccessInventory->GetCost()<<endl;
}
OutFile.close();
}