Hello,
I have the Fill, Show, Remove, and Find functions working properly, but I cannot get my WriteToFile function to work. I know that there is a problem involving WriteToFile("Success"); but I don't know how to fix it so that my void WriteToFile function will work. Any help would be appreciated! Thanks in advance
Here is my code so far:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class InventoryInfo
{
string InventoryID;
string InventoryDesc;
string InventoryCost;
public:
InventoryInfo() {InventoryID=""; InventoryDesc=""; InventoryCost="";}
InventoryInfo(string ID, string Desc, string Cost)
{
InventoryID=ID;
InventoryDesc=Desc;
InventoryCost=Cost;
}
void SetID(string ID) {InventoryID=ID;}
void SetDesc(string Desc) {InventoryDesc=Desc;}
void SetCost(string Cost) {InventoryCost=Cost;}
string GetID() {return InventoryID;}
string GetDesc() {return InventoryDesc;}
string GetCost() {return InventoryCost;}
};
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);
int main()
{
vector<InventoryInfo> iInfo; //Creates an object...vector of strings...
Fill(iInfo);
Show(iInfo);
Remove(iInfo);
Show(iInfo);
Find(iInfo);
iInfo.WriteToFile("Success");
}
void Fill(vector<InventoryInfo>& iInfo)
{
cout<<"*** Add item ***"<<endl;
string ID, Desc;
string Cost;
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<<"=================================================================="<<endl;
InventoryInfo Value0;
Value0.SetID(ID);
Value0.SetDesc(Desc);
Value0.SetCost(Cost);
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<<"=================================================================="<<endl;
}
}
void Find(vector<InventoryInfo> iInfo)
{
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<<"=================================================================="<<endl;
return;
}
}
cout<<"\nItem ID not found, please try again..."<<endl;
return;
}
void Remove(vector<InventoryInfo>& iInfo)
{
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(iInfo.c_str(), 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();
}