Thread: Data Question about Reading Files: Encapsulated Inventory Program in C++

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    73
    I'm getting some compiler errors trying to read from the file. It is saying that 'record' has not been declared although I clearly declared it in main() as an instance of the class inventory as I've always done for programs involving classes.

    Here is my code as well as an attached .cpp version of the program for anybody wishing to run it. I also attached the .txt data file that I am reading in which as you will see is reading in every field separated by either a comma delimiter or new line which brings you to the next record in the file.

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <cstdlib>
    #include <conio.h>
    #include <iomanip>
    
    
    class Inventory {
    
       private:
          int ProdID, qty_stock, reord_lev;
          string ProdDesc, manu;
          float unitcost;
          
       public:
          Inventory(int, string, string, float, int, int);
          void listcurinv_1();
          void listcurinv_2();
          void add_stock();
          void rmv_stock();
          void add_prod();
          void rmv_prod();
          void calc_inv();
          void list_reord();
    };
    
    
    Inventory::Inventory(int id, string desc, string manufac, float cost, int qty, int r_level) {
       ProdID = id;
       ProdDesc = desc;
       manu = manufac;
       unitcost = cost;
       qty_stock = qty;
       reord_lev = r_level;
    }
    
    
    void Inventory::listcurinv_1() {
       int i = 0;
       ifstream infile("a:\\CO856 Assignment 1\\sportsstore.txt");
    
       // initial read of data
       infile.getline(record[i].ProdID, 10, ',');
       infile.getline(record[i].ProdDesc, 30, ',');
       infile.getline(record[i].manu, 15, ',');
       infile.getline(record[i].unitcost, 10, ',');
       infile.getline(record[i].qty_stock, 5, ',');
       infile.getline(record[i].reord_lev, 5);
       i++;
    
    
       // test for either EOF or 100 records in the file and stop reading 
          records when one of those statements is true
       while (!infile.eof() || i < 100) {
          infile.getline(record[i].ProdID, 10, ',');
          infile.getline(record[i].ProdDesc, 30, ',');
          infile.getline(record[i].manu, 15, ',');
          infile.getline(record[i].unitcost, 10, ',');
          infile.getline(record[i].qty_stock, 5, ',');
          infile.getline(record[i].reord_lev, 5);
          i++;
       }
    
       // sort by product description
       string spare;
       for (int j = 0; j < i-1; j++) {
          for (int k = 0; k < i; k++) {
             if (record[j].ProdDesc > record[k].ProdDesc) {
                spare = record[j].ProdDesc;
                record[j].ProdDesc = record[k].ProdDesc;
                record[k].ProdDesc = spare;
             }
          }
       }
    
       i = 0;
    
       // display records on screen
       while (!infile.eof() || i < 100) {
          cout << setw(10) << record[i].ProdID;
          cout << setw(30) << record[i].ProdDesc;
          cout << setw(15) << record[i].manu;
          cout << setw(10) << record[i].unitcost;
          cout << setw(5) <<  record[i].qty_stock;
          cout << setw(5) <<  record[i].reord_lev << endl;
       }
    }
    
    int main() {
       Inventory record[100];
       record.listcurinv_1();
       getche();
       return 0;
    }
    This is probably one of the last obsctales I need to get through to be well on my way with this program. How is it not recognizing 'record' as being declared when I have it declared in main()?

    If anyone has any advice or help to give for this problem, that would be great. Thanks.
    Last edited by goron350; 06-10-2005 at 03:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading data contents from files in c++
    By shaheel in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2008, 09:02 PM
  2. Reading large complicated data files
    By dodzy in forum C Programming
    Replies: 16
    Last Post: 05-17-2006, 04:57 PM
  3. Question about IOStream and reading strings from files
    By kingpinzs in forum C++ Programming
    Replies: 22
    Last Post: 12-13-2005, 11:29 AM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. reading data format into program
    By lambs4 in forum C Programming
    Replies: 1
    Last Post: 10-23-2003, 02:27 PM