-
Write a Vector to a File
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:
Code:
#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();
}
-
std::vector has no c_str method.
-
Thanks for replying! How would I go about fixing this?
-
Uh, well the problem is this:
You probably want the function (maybe) to take the contents to write to file (which would be a vector) and a string telling the filename of the file to write to (that would be a string).
A vector is a collection of one or more types (ie it is an array), so obviously it has no c_str() method!
I think you need to think this over a little bit more on what you actually want to do.
I also notice to try to call a member WriteToFile on the vector! Vector has no such method!
I suspect you want to write it to a file, but it seems to me like you really need to study a bit more how objects work.
You can't just declare a global method and expect a member function in an object to pop up.
-
Thanks for replying! Okay, so I've made the following changes, but still no dice:
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class InventoryInfo
{
string InventoryID;
string InventoryDesc;
string InventoryCost;
string FileName;
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;}
};
class FileAccess
{
string FileName;
int Size;
public:
FileAccess();
FileAccess(string);
void WriteToFile();
}
FileAccess::FileAccess()
{
FileName = "Temp File";
Size = 0;
}
FileAccess::FileAccess(string FName)
{
FileName = FName;
Size = 0;
}
void Fill(vector<InventoryInfo>& iInfo);
void Show(vector<InventoryInfo> iInfo);
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);
WriteToFile(iInfo);
}
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 FileAccess::WriteToFile()
{
fstream OutFile(FileName.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();
}
-
Now you should create an instance FileAccess in main and call its WriteToFile method. That method also needs to take the vector as an argument.
Incidentally, if your function doesn't modify the vector (or any other potentially large object) it is best to pass them by const reference to avoid unnecessary copying.
E.g:
Code:
void Find(const vector<InventoryInfo>& iInfo);
-
Thank you anon for replying! What do you mean by "create an instance FileAccess in main and call its WriteToFile method"? Sorry if I may seem slow, but I am new to programming.
-
It seems that you have made WriteToFile a member function of a class called FileAccess. So you need a FileAccess instance to call it with:
Code:
int main()
{
vector<InventoryInfo> iInfo;
FileAccess file("somewhere");
...
file.WriteToFile(iInfo);
}
-
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();
}
-
Oh! Never mind, I caught my mistake:
Value0.SetCost(Cost);
Value0.SetCost(Loc);
Value0.SetCost(Stock);
They were all SetCost -_-
Thanks again for your help, appreciated it!