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

Well without giving you the code directly, since it's usually far easier to learn by doing it yourself. iInfo doesn't have a member called WriteToFile(), "Success" isn't an iInfo object, which is what the function expects as a parameter & then OutFile, well the iInfo.c_str() isn't a string to a valid directory path for you to write out to file.

What you need to do is determine a valid directory path & pass that to the WriteToFile function aswell. (Or alternatively you can include the path within iInfo) Check that the file is then open & proceed to write to it.

I've made the following changes, I'm still stuck : /

#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();
}

My answer belongs to the post #1.

>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");

1. WriteToFile is not a member function and its argument datatype mismatch.

iInfo.WriteToFile("Success");  // Invalid.

2. Missing return value - main function

int main() {
   .....
   return 0;
 }

3. With WriteToFile () - Filename?

fstream OutFile(iInfo.c_str(), ios::out);

Thanks adatapost for replying! I have made recommended changes on post #3, but the program still won't run

to add text into an already existing file use the

ios::app

function

and to read a file use the

ios::in

function

I have modified program source written at post #1.

#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);
		 WriteToFile(iInfo);
   return 0;
}

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("t.txt" ,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();
}

Thanks so much adatapost! 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?

#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();
}

>Do you know what's wrong with it?
I know what will be wrong with you if I solved whole program myself.

Your program has no problem but the problems are with you. I hope you get it.

Oh! Never mind, I caught my mistake:

Value0.SetCost(Cost);
Value0.SetCost(Loc);
Value0.SetCost(Stock);

They were all SetCost -_-

I was spoon fed I know : /, but I was honestly lost on the write() function! Thanks again for your help!

Thanks.
Mark this thread as Solved if you get solution.

Thanks and goodbye

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.