Can someone tell me why my code is not writing to a data file! I get no errors and everything looks great!

#include "Account.h"
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit function

using std::ofstream;


int main() {
	ofstream outdata;
	const int SIZE = 6;

	//Account array
	Account Account_array[SIZE] = { Account(832442, 560.10, .02),
									Account(832443, 1020.58, .04),
									Account(832444, 78.00, .02),
									Account(832447, 1200.12, .04),
									Account(832449, 489.33, .02),
									Account(833001, 105.74, .02)};
		
	// Loop to display Account details
		for (int x=0; x<6; x++) {
			Account_array[x].account_details();
		}

		outdata.open("AccountDetails.dat"); // Opens the file
		if ( !outdata ) {	//File could not be opened
			cout << "Error: file could not be opened" << endl;
			exit(1);
		

		for (int x=0; x<6; x++) {
			Account_array[x].account_details();
			outdata.close();
		}
		}
			return 0;
		}
#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();
};

Account::Account()
{
	number =0;
	balance = 0;
	rate = 0;
}

Account::Account(int number, double balance, double rate) {
                Account::number = number;
                Account::balance = balance;
				Account::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";
}

It doesn't write to the file because account_details() only writes to the screen. To fix that I would add a parmeter which would be either cout or an ofstream object

void Account::account_details(ostream& out)
{
   out << // blabla
}

example:

#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;

#include<string>

using namespace std;

void foo(ostream& out)
{
	out << "Hello World\n";
}

int main()
{
	foo(cout);
	ofstream out("myfile.txt");
	foo(out);
}

If i do this

void Account::account_details(ostream& out) {
	out << "The current account number is " << number <<
		" with a balance of $" << balance << " and an interest rate of " << rate << ".\n\n";
}

I get the error that deceleration is incompatible!

Did you also add the parameter it in the class declaration for that function?

Yes

Is this better??

#include "Account.h"
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit function

using std::ofstream;

int main() {


	ofstream outdata;
	const int SIZE = 6;


	//Account array
	Account Account_array[SIZE] = { Account(832442, 560.10, .02),
									Account(832443, 1020.58, .04),
									Account(832444, 78.00, .02),
									Account(832447, 1200.12, .04),
									Account(832449, 489.33, .02),
									Account(833001, 105.74, .02)};
		
	// Loop to display Account details
		for (int x=0; x<6; x++) {
			Account_array[x].account_details();
		


		ofstream myfile;
		myfile.open ("Account.data");
		myfile << Account_array[x].account_details();
		myfile.close();
		}

		}


			return 0;
		}

>>Is this better??

No. line 31 needs to pass the ofstream object as a parameter Account_array[x].account_details(myfile);

How is this

#include "Account.h"
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit function

using std::ofstream;

int main() {


	ofstream outdata;
	const int SIZE = 6;


	//Account array
	Account Account_array[SIZE] = { Account(832442, 560.10, .02),
									Account(832443, 1020.58, .04),
									Account(832444, 78.00, .02),
									Account(832447, 1200.12, .04),
									Account(832449, 489.33, .02),
									Account(833001, 105.74, .02)};
		
	// Loop to display Account details
		for (int x=0; x<6; x++) {
			Account_array[x].account_details();
		}


		ofstream file ("AccountDetail.data");
		if(!file)
        {
                cerr<<"ERROR"<<endl;
                exit(0);
        }
		Account_array[SIZE].account_OutPutdetails(file);
		file.close();

		cin.ignore();
		cin.get();


			return 0;
		}

My data file is showing as this when I open in a text pad.

-858993460
-9.25596e+061
5.43402e-304

line 35: >> Account_array.account_OutPutdetails(file);

Nope. Change SIZE to 0, such as Account_array[0].account_OutPutdetails(file);

How is this

My data file is showing as this when I open in a text pad.

-858993460
-9.25596e+061
5.43402e-304

Are those numbers correct? If so, it may be working. If not, it's not working.

Got it to work guys but just out of curiosity lets say I wanted to read the data in now and add 2 more accounts to it or edit one of the acocunts. How could I do this?

#include "Account.h"
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit function

using std::ofstream;

int main() {


	ofstream outdata;
	const int SIZE = 6;


	//Account array
	Account Account_array[SIZE] = { Account(832442, 560.10, .02),
									Account(832443, 1020.58, .04),
									Account(832444, 78.00, .02),
									Account(832447, 1200.12, .04),
									Account(832449, 489.33, .02),
									Account(833001, 105.74, .02)};
		
	// Loop to display Account details
		for (int x=0; x<6; x++) {
			Account_array[x].account_details();
		}


		ofstream file ("AccountDetail.data");
		if(!file)
        {
                cerr<<"ERROR"<<endl;
                exit(0);
        }
		for(int i=0; i<SIZE; i++)
                Account_array[i].account_OutPutdetails(file);

		file.close();

		cin.ignore();
		cin.get();


			return 0;
		}

read it back in the opposite direction that it was written.

Like this??

#include "Account.h"
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit function

using std::ofstream;

int main() {


	ifstream readData;

		ifstream file("AccountDetail.data");
		if(!file)
        {
                cerr<<"ERROR"<<endl;
                exit(0);
        }
		Account::account_Readdetails(file);

		file.close();

		cin.ignore();
		cin.get();


			return 0;
		}

EDIT

Yes, like that. But its going to be a little difficult getting the variables back because you put so much verbiage into the file. Only write the data and it will be easy to read back

void Account::account_details(ostream& out) {
	out << number << " " << balance << " " << rate << "\n";
}

Here is the code I have and for some reason when i run it the command prompt appears blank. I changed my write statement to the method you said to!

Here is how the file looks:

832442 560.1 0.02
832443 1020.58 0.04
832444 78 0.02
832447 1200.12 0.04
832449 489.33 0.02
833001 105.74 0.02

#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)
		{
			in >> number >> balance >> rate;
		}
	
};

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";
}
#include "Account.h"
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit function

using std::ofstream;

int main() {

	Account acc;
	ifstream readData;

		ifstream file("AccountDetail.data");
		if(!file)
        {
                cerr<<"ERROR"<<endl;
                exit(0);
        }
		acc.account_Readdetails(file);
		file.close();

		cin.ignore();
		cin.get();


			return 0;
		}

>> for some reason when i run it the command prompt appears blank.
You have forgotten to use account_details() .

Sweet here my code that works

#include "Account.h"
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit function

using std::ofstream;

int main() {

	Account acc;
	ifstream readData;

		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();
		}

		file.close();

		cin.ignore();
		cin.get();


			return 0;
		}

Now i just need to figure out how to edit accounts in the file then write them back in all while not over writing them

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:

#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;
		}
#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

1>\main.cpp(71): error C2676: binary '[' : 'Account' does not define this operator or a conversion to a type acceptable to the predefined operator

In main() , you have declared

Account acc;

That gives you a single instance of class Account but you are trying to use it as if it were an array of pointers to Account objects, hence the error. Perhaps try to employ an array of Account , or if you are familiar with std::vector , then a std::vector<Account> might also do.

Then sorry to say, but as to the rest of the main() , currently your code is so poorly formatted that the program flow is simply undetectable. If you are using Visual Studio 2005 or later, try auto-formatting the code by pressing;
Ctrl+A Ctrl+K Ctrl+F
to realize the difference. Furthermore, you could;
- make sure that the editor uses spaces instead of tabs
- configure the 'indent size' to a small number (e.g. 2)
see Tools / Options / Text Editor / C/C++ for these options.

Ok I got my code to accept user input....but it is printing the information wrong! I will include my code and my command prompt problem so you can see it! Oh and the reason the program was not working...was cause I was not assigning the value to anything! It is filling everything with a 0 except for the new account!

#include "Account.h"
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit function

using std::ofstream;

int main() {

	Account accounts[8];
	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 1 new accounts
		for (int i = 0; i <1; i++) {
			cout << "Enter the New Account Number: \n";
			cin >> tempInt;
			accounts[6].setNumber(tempInt);

			cout << "Enter the New Accounts Balance: \n";
			cin >> tempDouble;
			accounts[6].setBalance(tempDouble);

			cout << "Enter the New Accounts Rate: \n";
			cin >> tempDouble;
			accounts[6].setRate(tempDouble);
		}
		 cout << "_______________________________________________________\n";
				//Add 1 new accounts
		for (int i = 0; i <1; i++) {
			cout << "Enter the New Account Number: \n";
			cin >> tempInt;
			accounts[7].setNumber(tempInt);

			cout << "Enter the New Accounts Balance: \n";
			cin >> tempDouble;
			accounts[7].setBalance(tempDouble);

			cout << "Enter the New Accounts Rate: \n";
			cin >> tempDouble;
			accounts[7].setRate(tempDouble);
		}
			for (int x=0; x<8; x++) {
			accounts[x].account_details();
		}

                        //User will be able to change the Account Balance 
                        cout << " The Balance of the account is: " << accounts[2].getBalance() << endl;
            cout << " Please enter the change to the account: ";
            cin >> tempDouble;
            accounts[2].setBalance(tempDouble);
            cout << "_______________________________________________________\n";


		for(int i=0; i<8; i++)
		accounts[i].account_details();

		file.close(); // close file



		cin.ignore();
		cin.get();


			return 0;
		}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.