Thread: Reading from and writing to a data file

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    20

    Reading from and writing to a data file

    Ok guys the follow code I am trying to read from the file!
    Add 2 New Accounts
    Then modify One Account
    Then Write everything back to the file!

    Can someone please help me...it does not work and I am getting this error:

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <functional>
    #include <algorithm>
    #include <string>
    #include <cstdlib>
    #include <sstream>
    using namespace std;
    
    //Account class
    class Account {
    private:
    	int number;
    	double balance;
    	double rate;
    
    public:
    	Account(); // Constructor
    	Account (int, double, double);
    	void setNumber(int);
        void setBalance(double);
        void setRate(double);
    
    	int getNumber();
        double getBalance();
        double getRate();
    	void account_details();
    	string string_account_details();
    	void account_Readdetails(ifstream &in);	
    	void Account::account_details(ostream& out) {
    	out << number << " " << balance << " " << rate << "\n";
    	}
    
    	
    };
    
    Account::Account()
    {
    	number =0;
    	balance = 0;
    	rate = 0;
    }
    
    Account::Account(int number, double balance, double rate) 
    {
    //this pointers to the current class
            this->number = number;
            this->balance = balance;
            this->rate= rate;
    }
    
    void Account::setNumber(int number) {
        Account::number = number;
    }
    
    void Account::setBalance(double balance) {
        Account::balance = balance;
    }
    
    void Account::setRate(double rate) {
        Account::rate = rate;
    }
    
    int Account::getNumber() {
        return number;
    }
    double Account::getBalance() {
        return balance;
    }
    double Account::getRate() {
        return rate;
    }
    
    void Account::account_details() {
    	cout << "The current account number is " << number <<
    		" with a balance of $" << balance << " and an interest rate of " << rate << ".\n\n";
    }
    void Account::account_Readdetails(ifstream &in)
    		{
    			in >> number >> balance >> rate;
    		}
    Code:
    #include "Account.h"
    #include <iostream>
    #include <fstream>
    #include <cstdlib> // for exit function
    
    using std::ofstream;
    
    int main() {
    
    	Account acc;
    	ifstream readData;
    	int tempInt;
    	double tempDouble;
    	int current_account = 0;
    	int menu = 0;
    
    		ifstream file("AccountDetail.data");
    		if(!file)
            {
                    cerr<<"ERROR"<<endl;
                    exit(0);
            }
    		acc.account_Readdetails(file);
    
    		for (int x=0; x<6; x++) {
    			acc.account_details();
    		}
    
    		//Add 2 new accounts
    		for (int i = 0; i <2; i++) {
    			cout << "Enter the New Account Number: ";
    			acc.setNumber(tempInt);
    
    			cout << "Enter the New Accounts Balance: ";
    			acc.setBalance(tempDouble);
    
    			cout << "Enter the New Accounts Rate: ";
    			acc.setRate(tempDouble);
    
    			for (int x=0; x<8; x++) {
    			acc.account_details();
    		}
    
    			//Change Account Information
    
    			         while (menu!=3)//keep going until 3 is selected
            {
            cout << "Vehicle Menu\n\n";                     //Menu
                    cout << "1. Select the Account to edit:\n";
                    cout << "2. Change the Account Number :\n";
                    cout << "3. Exit the Program:\n\n\n";
    
            cout << "Please choose one of the above options: ";
            cin >> menu;
            cin.ignore();
            cout << "_______________________________________________________\n";
    			 //switch begin
            switch ( menu )
                     {
            case 1:
                            //Here the user will select which Account to edit!
                            cout << " Pick a Account to edit\n";
                            cin >> current_account;
                --current_account;
                cin.ignore();
                cout << "_______________________________________________________\n";
                break;
    
            case 2:
                            //User will be able to change the Account Balance 
                            cout << " The Balance of the account is: " << acc[current_account]->getBalance() << endl;
                cout << " Please enter the change to the account: ";
                cin >> tempDouble;
                acc[current_account]->setBalance(tempDouble);
                cout << "_______________________________________________________\n";
                break;
    
    			  default:
                cout << " Please restart the program, you have entered invalid information!\n\n\n";
                break;
    		}
    					 }
    
    		for(int i=0; i<8; i++)
    		acc[i].account_OutPutdetails(file);
    
    		file.close(); // close file
    
    
    
    		cin.ignore();
    		cin.get();
    
    
    			return 0;
    		}
    Error:

    1>\main.cpp(71): error C2676: binary '[' : 'Account' does not define this operator or a conversion to a type acceptable to the predefined operator
    1>\main.cpp(71): error C2227: left of '->getBalance' must point to class/struct/union/generic type
    1>\main.cpp(74): error C2676: binary '[' : 'Account' does not define this operator or a conversion to a type acceptable to the predefined operator
    1>\main.cpp(74): error C2227: left of '->setBalance' must point to class/struct/union/generic type
    1>\main.cpp(85): error C2676: binary '[' : 'Account' does not define this operator or a conversion to a type acceptable to the predefined operator
    1>\main.cpp(85): error C2228: left of '.account_OutPutdetails' must have class/struct/union

  2. #2
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    1>\main.cpp(71): error C2676: binary '[' : 'Account' does not define this operator or a conversion to a type acceptable to the predefined operator

    You can't referance a single item as an array

    1>\main.cpp(71): error C2227: left of '->getBalance' must point to class/struct/union/generic type
    You aren't using a pointer so -> is not defined you MUST use '.'

    repeat 2 more times.

Popular pages Recent additions subscribe to a feed