Thread: File reading

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    36

    File reading

    I need to write this code to take customer info until they select to stop, then display all the records. My problem is thta I am only displaying the last record. How do I make it print all the records?



    Code:
    #include <string>
    #include <fstream>
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class Account
    {
    private:
     string name,
            address,
         cityStateZip,
         telephone_Number,
         date_Last_Payment;
     double account_Balance;
     ofstream File;
    public:
    	Account()
    	{
    		name="";
    			address="";
    			cityStateZip="";        
    			telephone_Number;    
    			date_Last_Payment;
    			account_Balance;	
    	}  
    	Account(string N,string A,string C,string T,string D, double Ab)
    	{		name=N;
    			address=A;
    			cityStateZip=C;
    			telephone_Number=T;
    			date_Last_Payment=D;
    			account_Balance=Ab;
    	}
     bool Write(); 
     bool Read();
     void Copy(Account & A) 
    	 {		this ->name=A.name;
    			this ->address=A.address;
    			this ->cityStateZip=A.cityStateZip;
    			this ->telephone_Number=A.telephone_Number;
    			this ->date_Last_Payment=A.date_Last_Payment;
    			this ->account_Balance=A.account_Balance;}
    
    	 void Cin()
    	 {
    		char ans;
    		do{
    		cout << "Enter Name: " << endl;
    		getline(cin,name);
    		fflush(stdin);
    		cout << "Enter adress: " << endl;
    		getline(cin,address);
    		fflush(stdin);
    		cout << "Enter City/State/zip: " << endl;
    		getline(cin,cityStateZip);
    		fflush(stdin);
    		cout << "Enter telephone number: " << endl;
    		getline(cin,telephone_Number);
    		fflush(stdin);
    		cout << "Enter date of last payment: " << endl;
    		getline(cin,date_Last_Payment);
    		fflush(stdin);
    		cout << "Enter account balance: " << endl;
    		cin >> account_Balance;
    
    		cout << "Do you have another account? (Y|N)" << endl;
    	    cin >> ans;
    	    cin.ignore();
     
    		}while (ans == 'y' || ans == 'Y');
    	 }
    
    	 void Cout()
    	 {
    
    		 Account A;
             fstream File;
    		 char again;
    		 while (!File.eof())
    		 {
    			 cout << "Name: ";
    			 cout << name << endl;
    			 cout << "Address: ";
    		     cout << address << endl;
    			 cout << "City/State/Zip: " << endl;
    			 cout << cityStateZip << endl;
    			 cout << "Telephone number: ";
    		     cout << telephone_Number << endl;
    			 cout << "Last Payment: " << endl;
    			 cout << date_Last_Payment << endl;
    			 cout << "Account Balance: " << endl;
    		     cout << account_Balance << endl;
    			 cout << "\nPress the Enter key to see the next record.\n";
    			 cin.get(again);
    		 }
    
    		 cout << "That's all the data in the file!\n";
    		 File.close();
    	 }
    };
    int main()
    {  
     fstream File;
     char ans;
     Account A;
    	A.Cin();
    	A.Write();
    	A.Read();	
    	A.Cout();
    	fflush(stdin);
     system("pause");
     return 0;
    }
    bool Account::Write ()
    {	 
    	File.open ("Account.txt",ios::app); 
    	char Tab='\t';
    	if(!File)
    		{ cout << " can't open output file"<<endl; return false;}
    	else File<< name << Tab
    				<<address<<Tab
    				<<cityStateZip<<Tab
    				<<telephone_Number<<Tab
    				<<date_Last_Payment<<Tab
    				<< account_Balance<<endl;
    				return true;
    }
    bool Account::Read()
    {
    	
    	fstream File;
    	char Tab='\t';
    	Account A;
    	File.read(reinterpret_cast<char *>(&A),sizeof(A));
    	File.close();
    	return false;
    	
    	
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You #include <vector> but you don't use one. You need to use a vector and call push_back to add each Account variable after you've filled it with the data from the user. Then you can loop through the vector and display the data.

    I'd also consider cleaning up the code a bit. The Read() function does nothing. Even if it did, it uses binary reading while your Write function outputs in text mode. You use fflush(stdin) which is not necessary and not defined. Your Cout function doesn't actually display any data. You use variables named File in a bunch of places where they aren't needed.

    I'd start smaller and get one thing working at a time. Try getting the console input and output for one Account working first. Then do more than one in a loop. Then maybe add the code with the vector to keep track of the ones you've already done and output them later.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    36
    The read function was part of the assignment. It is supposed to bring the info into memory. I have never used vectors before, and am so confused about passing and displaying them! Here's what I have done:

    Code:
    #include <string>
    #include <fstream>
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class Account
    {
    private:
            string name,
            address,
            cityStateZip,
            telephone_Number,
            date_Last_Payment;
            double account_Balance;
            ofstream File;
    public:
    	Account()
    	{
                name="";
    			address="";
    			cityStateZip="";        
    			telephone_Number;    
    			date_Last_Payment;
    			account_Balance;	
    	}  
    	Account(string N,string A,string C,string T,string D, double Ab)
    	{		
                name=N;
    			address=A;
    			cityStateZip=C;
    			telephone_Number=T;
    			date_Last_Payment=D;
    			account_Balance=Ab;
       }
       bool Write(); 
       void Copy(Account & A) 
       {	    	
                this ->name=A.name;
    			this ->address=A.address;
    			this ->cityStateZip=A.cityStateZip;
    			this ->telephone_Number=A.telephone_Number;
    			this ->date_Last_Payment=A.date_Last_Payment;
    			this ->account_Balance=A.account_Balance;
       }
    
    const std::vector< std::string > Cin()
       {
    		char ans;
    		do{
    		   cout << "Enter Name: " << endl;
    		   getline(cin,name);
    		   fflush(stdin);
    		   cout << "Enter adress: " << endl;
    		   getline(cin,address);
    		   fflush(stdin);
    		   cout << "Enter City/State/zip: " << endl;
               getline(cin,cityStateZip);
               fflush(stdin);
               cout << "Enter telephone number: " << endl;
               getline(cin,telephone_Number);
               fflush(stdin);
               cout << "Enter date of last payment: " << endl;
        	   getline(cin,date_Last_Payment);
    		   fflush(stdin);
    		   cout << "Enter account balance: " << endl;
    		   cin >> account_Balance;
    		      cout << "Do you have another account? (Y|N)" << endl;
    	       cin >> ans;
    	       cin.ignore();
    		   	}while (ans == 'y' || ans == 'Y');
               fstream File;
               vector<string> list;
               string word;
       	       ifstream file("account.txt");
               while(file >> word)
               {
                        list.push_back(word); 
               }
    	       return list;
    }
    
    void Cout( std::vector< std::string > list)
    	 {
    		 Account A;
    		 char again;
    		 int count=0;
    		 while (!File.eof())
    		 {
              
                       for (int i=0; i< list.size();i++){
    			               cout << "Record number " << i+1 <<": ";
    			               for (int h=0;h< 6;h++)
                               {
    			                   cout << endl << list[h] << endl;
                               }
    			               cout << "\nPress the Enter key to see the next record.\n";
    			               cin.get(again);
                        }
    		 }
    		 cout << "That's all the data in the file!\n";
    		 File.close();
    	 }
    };
    
    int main()
    {  
        vector<string> list;
        int count;
        fstream File;
        char ans;
        Account A;
    	A.Cin();
    	A.Write();
    	A.Cout(list);
        system("pause");
    }
    
    bool Account::Write ()
    {	 
    	File.open ("Account.txt",ios::app); 
    	char Tab='\t';
    	if(!File)
    	{ 
                 cout << " can't open output file"<<endl; 
                 return false;
        }
    	else File<< name << Tab
    				<<address<<Tab
    				<<cityStateZip<<Tab
    				<<telephone_Number<<Tab
    				<<date_Last_Payment<<Tab
    				<< account_Balance<<endl;
    				return true;
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You want your vector to hold Accounts, not strings. Each new account will be added to the vector.

    You shouldn't be returning the vector from the Cin function, you should be creating an Account variable in main (or somewhere outside the Account class) and pushing it back on to the vector. I would create a loop in main that includes the Account A declarations and the rest of the functions you need to call.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    36
    How can i input account into the vector?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. Make a vector< Account >.
    2. Read in an account using the Cin thing.
    3. Use push_back to push that account onto the vector.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You already have code that adds a string to a vector. Do the same thing, but use Account instead of string.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    36
    I changed the vector to get Accounts, and now i have this errors:

    no match for 'operator>>' in 'file >> word'


    Code:
    #include <string>
    #include <fstream>
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class Account
    {
    private:
            string name,
            address,
            cityStateZip,
            telephone_Number,
            date_Last_Payment;
            double account_Balance;
            ofstream File;
    public:
    	Account()
    	{
                name="";
    			address="";
    			cityStateZip="";        
    			telephone_Number;    
    			date_Last_Payment;
    			account_Balance;	
    	}  
    	Account(string N,string A,string C,string T,string D, double Ab)
    	{		
                name=N;
    			address=A;
    			cityStateZip=C;
    			telephone_Number=T;
    			date_Last_Payment=D;
    			account_Balance=Ab;
       }
       bool Write(); 
       void Copy(Account & A) 
       {	    	
                this ->name=A.name;
    			this ->address=A.address;
    			this ->cityStateZip=A.cityStateZip;
    			this ->telephone_Number=A.telephone_Number;
    			this ->date_Last_Payment=A.date_Last_Payment;
    			this ->account_Balance=A.account_Balance;
       }
    
    const std::vector< std::string > Cin()
       {
    		char ans;
    		do{
    		   cout << "Enter Name: " << endl;
    		   getline(cin,name);
    		   fflush(stdin);
    		   cout << "Enter adress: " << endl;
    		   getline(cin,address);
    		   fflush(stdin);
    		   cout << "Enter City/State/zip: " << endl;
               getline(cin,cityStateZip);
               fflush(stdin);
               cout << "Enter telephone number: " << endl;
               getline(cin,telephone_Number);
               fflush(stdin);
               cout << "Enter date of last payment: " << endl;
        	   getline(cin,date_Last_Payment);
    		   fflush(stdin);
    		   cout << "Enter account balance: " << endl;
    		   cin >> account_Balance;
    		      cout << "Do you have another account? (Y|N)" << endl;
    	       cin >> ans;
    	       cin.ignore();
    		   	}while (ans == 'y' || ans == 'Y');
               fstream File;
               vector<Account> list;
               Account word;
       	       ifstream file("account.txt");
               while(file >> word)
               {
                        list.push_back(word); 
               }
    	       return list;
    }
    
    void Cout( std::vector< std::string > list)
    	 {
    		 Account A;
    		 char again;
    		 int count=0;
    		 while (!File.eof())
    		 {
              
                       for (int i=0; i< list.size();i++){
    			               cout << "Record number " << i+1 <<": ";
    			               for (int h=0;h< 6;h++)
                               {
    			                   cout << endl << list[h] << endl;
                               }
    			               cout << "\nPress the Enter key to see the next record.\n";
    			               cin.get(again);
                        }
    		 }
    		 cout << "That's all the data in the file!\n";
    		 File.close();
    	 }
    };
    
    int main()
    {  
        vector<string> list;
        int count;
        fstream File;
        char ans;
        Account A;
    	A.Cin();
    	A.Write();
    	A.Cout(list);
        system("pause");
    }
    
    bool Account::Write ()
    {	 
    	File.open ("Account.txt",ios::app); 
    	char Tab='\t';
    	if(!File)
    	{ 
                 cout << " can't open output file"<<endl; 
                 return false;
        }
    	else File<< name << Tab
    				<<address<<Tab
    				<<cityStateZip<<Tab
    				<<telephone_Number<<Tab
    				<<date_Last_Payment<<Tab
    				<< account_Balance<<endl;
    				return true;
    }

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You are a long way away from making this work. It's going to take a lot more than small changes.

    I would start from scratch and implement one small thing at a time. I've mentioned a bunch of other changes that need to be made if you continue with this code (including changes on how to use the vector with Account that you didn't implement). Don't expect anything to work until you make some more changes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM