Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Write Vector to a File (http://www.go4expert.com/forums/write-vector-file-t18145/)

RiceFiend 22Jun2009 11:18

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


SaswatPadhi 22Jun2009 12:54

Re: Write Vector to a File
 
I think there are quite a few mistakes in your code :
(1) InventoryInfo class does not have any member func named WriteToFile !
Code:

int main()
{
        vector<InventoryInfo> iInfo;  //Creates an object...vector of strings...
        Fill(iInfo);
        Show(iInfo);
        Remove(iInfo);
        Show(iInfo);
        Find(iInfo);
        iInfo.WriteToFile("Success");
}

So, use it like : WriteToFile(iInfo);

(2) Vector does not have a member named c_str() !! Strings have it.
Code:

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

I really don't understand what you wanted to do here ! :crazy:
Why did you need to pass "Success" into this func ?? Did you intend it to write to a file named "Success" ??
In that case, change the line to fstream OutFile("Success", ios::out); and don't pass "Success" as an argument, rather pass iInfo as the argument.

RiceFiend 22Jun2009 13:27

Re: Write Vector to a File
 
Thanks for replying! I have made the following changes, but still no luck : /

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


SaswatPadhi 22Jun2009 17:58

Re: Write Vector to a File
 
You program still has errors !!

(1) You WriteToFile does not accept the iInfo vector ?!?
Code:

class FileAccess 
{
        string FileName;
        int Size;
public:
        FileAccess();
        FileAccess(string);
        void WriteToFile();
}

Change the marked line to void WriteToFile(vector<InventoryInfo>& iInfo);
Further change it in the definition.

(2) You cannot use WriteToFile(iInfo) directly 'cuz it's the part of a class.
Code:

int main()
{
        vector<InventoryInfo> iInfo;  //Creates an object...vector of strings...
        Fill(iInfo);
        Show(iInfo);
        Remove(iInfo);
        Show(iInfo);
        Find(iInfo);
        WriteToFile(iInfo);
}

Change the main function to :
Code:

int main()
{
        vector<InventoryInfo> iInfo;  //Creates an object...vector of strings...

        Fill(iInfo);
        Show(iInfo);
        Remove(iInfo);
        Show(iInfo);
        Find(iInfo);

        FileAccess MyFile("xyz");
        MyFile.WriteToFile(iInfo);
}

The above code will write to a file named "xyz". You may change it to as you like.

And BTW, the comment you have put for the first line : "//Creates an object...vector of strings..."; what does it mean ??? The correct one should be : "//Creates a vector of objects...".

Further, why do you need the int Size in the FileAccess Class ?? You can live without it, safely.
And, You are making this unnecessarily complicated by using classes. You could have changed the definition of WrieToClass this way :

Code:

void WriteToFile(vector<InventoryInfo>& iInfo, string FileName = "Temp File")
{
        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();
}

and called it this way :
Code:

int main()
{
        vector<InventoryInfo> iInfo;  //Creates an object...vector of strings...

        Fill(iInfo);
        Show(iInfo);
        Remove(iInfo);
        Show(iInfo);
        Find(iInfo);
        WriteToFile(iInfo, "xyz"); // Writes vector to "xyz" file.
}

Best of luck ! :)


All times are GMT +5.5. The time now is 06:16.