Thread: fstream/datafile problem

  1. #1
    val
    Guest

    Question fstream/datafile problem

    Perhaps someone can help me here. I have been looking over this code and I cannot figure why it is giving me an error(s). If someone can give me an alternative way I would greatly appreciate it.

    The program is not entirely finished. The program is basically about classes which reads and writes into a datafile (pa8.dat).

    Code:
    #include <string>
    #include <vector>
    #include <iostream.h>
    #include <iomanip.h>
    #include <stdio.h>
    #include <fstream.h>
    #include <stdlib.h>
    
    
    
    /**********************************base class REObject*******************/
    class REObject
    {
    public:
      int block; //location var
      int lot; //location var
      float price; //price var
      int sq_footage; //duh
    
      virtual void Print(); //overridden in subclasses
      virtual void Write(); //overridden in subclasses
    
    private:
    };
    
    //member methods - to be overridden!
    void REObject::Print() {cout<<"print REObject"<<endl;}
    void REObject::Write() {cout<<"write REObject"<<endl;}
    
    /**************** ONE FAMILYderived from REObject ***********************/
    class OneFamily : public REObject
    {
    public:
      void Print(); //overridden
      void Write(); //overridden
    private:
      int bathrooms;
      int baths;
      int year_built;
    };
    
    //member methods
    void OneFamily::Print() {cout<<"print OneFamily"<<endl;}
    void OneFamily::Write() {cout<<"write OneFamily"<<endl;}
    
    
    /**************** COMMERCIAL derived from REObject *************************/
    class Commercial : public REObject
    {
    public:
      void Print(); //overridden
      void Write(); //overridden
    private:
      char type_of_commerce;
      bool sub_dividable;
    };
    
    //member methods
    void Commercial::Print() {cout<<"print Commercial"<<endl;}
    void Commercial::Write() {cout<<"write Commercial"<<endl;}
    
    
    /***************** MULTIPLE DWEELING derived from REObject *******************/
    class MultipleDwelling : public REObject
    {
    public:
      void Print(); //overridden
      void Write(); //overridden
    private:
      int num_of_units;
    };
    
    //member methods
    void MultipleDwelling::Print() {cout<<"print MultipleDwelling"<<endl;}
    void MultipleDwelling::Write() {cout<<"write MultipleDwelling"<<endl;}
    
    /******************** REDBASE ************************/
    class REDbase
    {
    public:
      REDbase(char *file); //constructor: get filename from th CLI, opens the file, 
                           //builds the dbase in an array of pointers...
      ~REDbase(); //destructor: writes the dbase back to file...
      void Add();
      void Print();
      void Save();
    private:
      //vector<REObject*> dbase; //an array of REObject pointers (our database)
      REObject* dbase[100]; //array of pointers to REobjects 
      char DataFile[50]; //data file name 
    };
    
    REDbase::REDbase(char *file)
    {
      ifstream ifdatafile;
      
      ifdatafile.open(file); 
      if(!ifdatafile)
        {
          cout<<"Couldn't open dictionary. Exiting..."<<endl;
          exit(1);
        }
      
      string word;
     
      while(ifdatafile>>word)
        {
          //parse the datafile
          if(!strcmp(word.c_str(),"s"))
    	{
    	  cout<<endl;
    	  cout<<"single dwelling: ";
    	  new OneFamily;
    	}
          else if(!strcmp(word.c_str(),"m"))
    	{
    	  cout<<endl;
    	  cout<<"multi dwelling :";
    	  new MultipleDwelling;
    	}
          else if(!strcmp(word.c_str(), "c"))
    	{
    	  cout<<endl;
    	  cout<<"commercial :";
    	  new Commercial;
    	}
          else { cout<<" "<<word<<" "; }
        }
      cout<<endl;
    }
    
    
    /*******************************MAIN************************************************/
    
    int main(int argc, char ** argv)
    {
      /* main calls class REObject member function constructor to open data file, parse the data
         and populate the database, filename is static! */
    
      new REDbase("pa8.dat");
    
      /* main the enters a UI loop, giving the option to add a listing, print all listings
         to the console, or saving the dbase to a file and then quitting the program. 
         main determines the input and then calls member functions of REDbase accordingly */
    
      return (0);
    }

    The Error Message

    Code:
    --------------------Configuration: pa8 - Win32 Debug--------------------
    Compiling...
    pa8.cpp
    c:\documents and settings\val\desktop\c++\pa8.cpp(102) : error C2065: 'string' : undeclared identifier
    c:\documents and settings\val\desktop\c++\pa8.cpp(102) : error C2146: syntax error : missing ';' before identifier 'word'
    c:\documents and settings\val\desktop\c++\pa8.cpp(102) : error C2065: 'word' : undeclared identifier
    c:\documents and settings\val\desktop\c++\pa8.cpp(107) : error C2228: left of '.c_str' must have class/struct/union type
    c:\documents and settings\val\desktop\c++\pa8.cpp(113) : error C2228: left of '.c_str' must have class/struct/union type
    c:\documents and settings\val\desktop\c++\pa8.cpp(119) : error C2228: left of '.c_str' must have class/struct/union type
    Error executing cl.exe.
    
    pa8.obj - 6 error(s), 0 warning(s)

    P.S I am using Visual C++ as the compiler.


    Thanks.

  2. #2
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    couple of those errors can be fixed by adding [code]using namespace std;[/quote]after your includes

  3. #3
    Val
    Guest
    Already tried that, and gives me theres errors.

    Code:
    Compiling...
    pa8.cpp
    c:\documents and settings\val\desktop\c++\pa8.cpp(95) : error C2872: 'ifstream' : ambiguous symbol
    c:\documents and settings\val\desktop\c++\pa8.cpp(106) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is 
    no acceptable conversion)
    c:\documents and settings\val\desktop\c++\pa8.cpp(106) : fatal error C1903: unable to recover from previous error(s); stopping compilation
    Error executing cl.exe.
    
    pa8.obj - 3 error(s), 0 warning(s)
    If I just do #include<fstream>
    I get this error.

    Code:
    --------------------Configuration: pa8 - Win32 Debug--------------------
    Compiling...
    pa8.cpp
    c:\documents and settings\val\desktop\c++\pa8.cpp(127) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is 
    no acceptable conversion)
    Error executing cl.exe.
    
    pa8.obj - 1 error(s), 0 warning(s)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM