FileIO Help

This is a discussion on FileIO Help within the C++ Programming forums, part of the General Programming Boards category; Greetings. I've encountered an error that I've never gotten before using the same code. I am confused. Also, I'm fairly ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    4

    FileIO Help

    Greetings. I've encountered an error that I've never gotten before using the same code. I am confused. Also, I'm fairly new to programming in general, so my code is probably not optimized for the most part, and I know for certain that there are unnecessary bits.

    I've commented the troublemaker with a big arrow, if anyone could point me in the right direction, it would be appreciated.

    The error: no match for 'operator<<' in 'outFile << [+Warehouse]- etc.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    #include <iomanip>
    #include <fstream>
    #include <vector>
    #include <windows.h>
    
    
    
    
    using namespace std;
    
    
    const long MAX_SIZE = 682;
    
    
    struct Stock //Makes a new datatype
    {
        string name; //Name..
        string snum; //Serial number
        string qty; // Quantity   
    };
    
    
    char userDecision();
    void addStock(vector<Stock>& Warehouse);
    void addStocks(vector<Stock>& Warehouse);
    void displayWarehouse(vector<Stock>& Warehouse);
    void saveRecord(vector<Stock>& Warehouse);
    void overwriteRecord(vector<Stock>& Warehouse);
    
    
    
    
    int main (char& decision)
    {
        Stock stock;
        vector<Stock> Warehouse;
        int stockCount = 0;
        
        
        bool run = true;
        do
        {
            char welcome[64] = {'W','e','l','c','o','m','e',' ','t','o',' ','V','i','r','t','u','a','W','a','r','e','h','o','u','s','e',' ','v','9','0','0','0','.','0','6','8',' ','-',' ','P','l','e','a','s','e',' ','r','e','p','o','r','t',' ','a','n','y',' ','e','r','r','o','r','s','.'};
            for (int i=0;i<64;i++)
        {
            cout << welcome[i];
            Sleep(48);
        }
            cout << endl << endl;
            switch ( userDecision() )
            {
                case 'A': addStock(Warehouse); break;
                case 'M': addStocks(Warehouse); break;
                case 'D': displayWarehouse(Warehouse); break;
                case 'S': saveRecord(Warehouse); break;
                case 'O': overwriteRecord(Warehouse); break;
                case 'Q': run = false; break;
                default : cout << "ERROR: Invalid VirtuaWarehouse v9000.068 decision making format - - - - - - - -" << endl;
            }
            system("cls");
            
        }while(run);
        
        system("cls");
        char termination[78] = {'V','i','r','t','u','a','W','a','r','e','h','o','u','s','e',' ','v','9','0','0','0','0','.','0','6','8',' ','p','r','o','g','r','a','m',' ','t','e','r','m','i','n','a','t','e','d',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.'};
        for (int i=0;i<78;i++)
        {
            cout << termination[i];
            Sleep(84);
        }
        
    }
    
    
    char userDecision()
    {
         char decision;
         Sleep(400);
         cout << "'A' : Add Stock" << endl << endl;
         Sleep(400);
         cout << "'M' : Add Multiple Stocks" << endl << endl;
         Sleep(400);
         cout << "'D' : Display VirtuaWarehouse Stocks" << endl << endl;
         Sleep(400);
         cout << "'S' : Add current list to Warehouse Records" << endl << endl;
         Sleep(400);
         cout << "" << endl << endl;
         Sleep(400);
         cout << "'Q' : Quit" << endl << endl;
         cin >> decision;
         cin.ignore(1);
         return toupper(decision);
    }
    
    
    void addStock(vector<Stock>& Warehouse)
    {
        system("cls");
        Stock stock;
        int i;
        
        cout << "Enter stock name: ";
        getline(cin, stock.name);
            
        cout << "Enter Serial Number: ";
        getline(cin, stock.snum);
        cout << "Enter Quantity: ";
        getline(cin, stock.qty);
            
            
        Warehouse.push_back(stock);
        cout << endl;
        
        
    }
    void addStocks(vector<Stock>& Warehouse)
    {
        system("cls");
        Stock stock;
        int i;
        
        while(true)
        {
            cout << "Enter stock name, or 'quit' to quit: ";
            getline(cin, stock.name);
            
            if(stock.name == "quit")
            {
                break;
            }
            
            cout << "Enter Serial Number: ";
            getline(cin, stock.snum);
            cout << "Enter Quantity: ";
            getline(cin, stock.qty);
            
            
            Warehouse.push_back(stock);
            cout << endl;
            
         } 
         
    }
    
    
    void displayWarehouse(vector<Stock>& Warehouse)
    {
        system("cls");
        Stock stock;
        int i;
        
        cout << "--------------------------------" << endl;
        for(int q = 0; q<Warehouse.size(); q++)
        {
            cout << "Name: " << setw(18) << Warehouse[q].name << endl;
            cout << "Serial Number: " << setw(9) << Warehouse[q].snum << endl;
            cout << "Quantity: " << setw(14) << Warehouse[q].qty << endl; 
             
            cout << "--------------------------------" << endl;     
        }
        
        
        cout << endl;
        system("pause");
    }
    void saveRecord(vector<Stock>& Warehouse)
    {
         int s = Warehouse.size();
         
         ofstream outFile;
         outFile.open("warehouse.txt", ios::app);
         for(int i=0; i<s; i++)
         {
         outFile << Warehouse[i]; // <----THIS guy and his trouble-making friend below.
         }
         outFile.close();
    }
    
    
    void overwriteRecord(vector<Stock>& Warehouse)
    {
         int s = Warehouse.size();
         
         ofstream fout("warehouse.txt");
         for(int i=0; i<s; i++)
         {
         fout << Warehouse[i] << endl; // <----Same problem.
         }
         fout.close();
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,837
    Have you overloaded the output operator<< for your stock class? If not you'll need to print each individual element of the structure.

    Also please explain why you're not using a string for the following line:
    Code:
        char termination[78] = {'V','i','r','t','u','a','W','a','r','e','h','o','u','s','e',' ','v','9','0','0','0','0','.','0','6','8',' ','p','r','o','g','r','a','m',' ','t','e','r','m','i','n','a','t','e','d',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.'};

    Jim

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    4
    Hey Jim,

    First off, thanks for the reply!

    If it's not too complicated to ask, what does 'overloading the output operator<< for the stock class' mean? Also, by 'print each individual element of the structure' would that be like instead of just having it print stock, have it print stock.name, stock.snum, and stock.qty?

    And I didn't use a string there, because when I tried that, it printed it out all at once, and I wanted it to type nice and slow-like, kind of like an old computer. Aesthetics~

    Matt

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,837
    If it's not too complicated to ask, what does 'overloading the output operator<< for the stock class' mean?
    You may want to start with this link: C++ Operator overloading.

    Also, by 'print each individual element of the structure' would that be like instead of just having it print stock, have it print stock.name, stock.snum, and stock.qty?
    Yes.

    And I didn't use a string there, because when I tried that, it printed it out all at once, and I wanted it to type nice and slow-like, kind of like an old computer. Aesthetics~
    Do you know you can print the individual characters of a string exactly as you are printing the array?

    Code:
          string welcome = "Welcome";
            for (int i=0;i<welcome.size();i++)
        {
            cout << welcome[i];
            Sleep(48);
        }
    Jim

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    4
    Quote Originally Posted by jimblumberg View Post
    Do you know you can print the individual characters of a string exactly as you are printing the array?

    Code:
          string welcome = "Welcome";
            for (int i=0;i<welcome.size();i++)
        {
            cout << welcome[i];
            Sleep(48);
        }
    Oh man.. I knew that too.. Thanks for that.

    Well, I'm going to get studying up on this overloading and element printing business, thanks so much for this.

    Matt

    P.S. If you wouldn't mind keeping a tab on this thread for at least the duration of tomorrow(tuesday), as I doubt I'd be able to learn/do anything worth asking about for the rest of this night, but may have more questions tomorrow. If not, no worries, you've done more than your part! Thanks again.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    4
    -Deleted updated post here due to my obvious stupidity-

    The individual element thing was a great fix, thanks Jim! I'll be sure to learn that overloading business sometime or other though, it's probably easier in the long run.


    /thread
    Last edited by Mattagatari; 04-08-2013 at 10:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FileIO
    By iiwhitexb0iii in forum C Programming
    Replies: 12
    Last Post: 04-03-2006, 11:11 AM
  2. help with fileio
    By wart101 in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2006, 02:48 PM
  3. Replies: 13
    Last Post: 03-07-2005, 03:49 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21