Classes Problem

I am doing a classes assignment and have managed to get most information down but im struggling with reading and writing from a .txt file, could someone please point me in the right direction.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "BankAccount.h" // gives us access to the BankAccount class
#include <stdio.h> // access to opening files
#include <iostream> // gives us access to simple input and output
using namespace std; // don't worry about namespaces yet

int main()
{
	void add_new_record()
	{

FILE *BankAccount;
	BankAccount = fopen("BankAccount.txt", 0);
	char AccountName;
	int AccountNumber;
	float balance;


cout << "\n********************************";
cout << "\nWELCOME TO THE BANK OF GLAMORGAN\n";
cout << "\n********************************";

	}

	
	
	return 0;

}//end of main 


BankAccount.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "BankAccount.h" // gives access to the BankAccount declarations
#include <iostream>
using namespace std;

BankAccount::BankAccount(void)
{
    AccountNumber = 0;
    AccountName = "";
    balance = 0.0;
}

BankAccount::BankAccount(int numberIn, string nameIn)
{
    AccountNumber = numberIn;
    AccountName = nameIn;
    balance = 0.0;
}


BankAccount::~BankAccount(void)
{
	// nothing do to
}

int BankAccount::getAccountNumber(void)
{
return AccountNumber;
}

string BankAccount::getAccountName(void)
{
return AccountName;
}

double BankAccount::getBalance(void)
{	
return balance;
}

void BankAccount::deposit(double amountIn)
{
	balance = balance + amountIn;
}

void BankAccount::withdraw(double amountIn)
{
	balance = balance - amountIn;
}

void BankAccount::setAccountNumber(int numberIn)
{
	AccountNumber=numberIn;
}

void BankAccount::setAccountName(string nameIn)
{
	AccountName=nameIn;
}


Bank Account Header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#pragma once
#include "string"
using namespace std; // don't worry about namespaces for now

class BankAccount
{
	// The attributes for this class
private:
	// private attributes cannot be accessed directly. 
	int AccountNumber;
	string AccountName;
	double balance;
	
// The methods for this class
public:
	// constructors (overloaded)
	BankAccount(int numberIn, string nameIn);
	BankAccount(void);

	// destructor
	~BankAccount(void);

	// accessor methods (getters)
	int getAccountNumber(void);
	string getAccountName(void);
	double getBalance(void);

	// mutator methods (setters)
	void deposit(double amountIn);
	void withdraw(double amountIn);
	void setAccountNumber(int numberIn);
	void setAccountName(string nameIn);
};
Ok I have followed the tutorial but I am still having problems.

The error message I get back when I try to run the program is:
1>------ Build started: Project: Bank Account, Configuration: Debug Win32 ------
1>Build started 12/02/2012 21:48:12.
1>InitializeBuildStatus:
1> Touching "Debug\Bank Account.unsuccessfulbuild".
1>ClCompile:
1> main.cpp
1>c:\users\james\desktop\bank account 2\bank account\main.cpp(25): error C2371: 'myfile' : redefinition; different basic types
1> c:\users\james\desktop\bank account 2\bank account\main.cpp(13) : see declaration of 'myfile'
1>c:\users\james\desktop\bank account 2\bank account\main.cpp(30): error C2065: 'AccountName' : undeclared identifier
1>c:\users\james\desktop\bank account 2\bank account\main.cpp(31): error C2065: 'AccountNumber' : undeclared identifier
1>c:\users\james\desktop\bank account 2\bank account\main.cpp(32): error C2065: 'Balance' : undeclared identifier
1> BankAccountClass.cpp
1>c:\users\james\desktop\bank account 2\bank account\bankaccountclass.cpp(59): warning C4356: 'std::_Iosb<_Dummy>::in' : static data member cannot be initialized via derived class
1> with
1> [
1> _Dummy=int
1> ]
1>c:\users\james\desktop\bank account 2\bank account\bankaccountclass.cpp(59): error C2143: syntax error : missing ';' before '|'
1>c:\users\james\desktop\bank account 2\bank account\bankaccountclass.cpp(59): error C2182: 'in' : illegal use of type 'void'
1>c:\users\james\desktop\bank account 2\bank account\bankaccountclass.cpp(59): error C2373: 'in' : redefinition; different type modifiers
1> c:\program files\microsoft visual studio 10.0\vc\include\xiosbase(100) : see declaration of 'in'
1> Generating Code...
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.96
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Here is the code that I am trying to run:

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "BankAccount.h" // gives us access to the BankAccount class
#include <stdio.h> // access to opening files
#include <iostream> // gives us access to simple input and output
#include <fstream>
using namespace std; 

int main()
{

	char AccountName;
	int AccountNumber;
	double Balance;

	// Writes to my txt file
	ofstream myfile ("BankAccount.txt");
	if (myfile.is_open())
	{
    myfile << "AccountName\n ";
    myfile << "AccountNumber:\n ";
	myfile << "Balance:\n ";
    myfile.close();
	}
	else cout << "File Error";

	// Reads from txt file
	string line;
	ifstream myfile ("BankAccount.txt");
	if (myfile.is_open())
	{
		while ( myfile.good() )

	cin >> AccountName;
	cin >> AccountNumber;
	cin >> Balance;

	 {
	 getline (myfile,line);
     cout << line << endl;
	 }

	 myfile.close();
	}


	else cout << "File Error";
	
	return 0;

}//end of main


BankAccount.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "BankAccount.h" // gives access to the BankAccount declarations
#include <iostream>
using namespace std;

BankAccount::BankAccount(void)
{
    AccountNumber = 0;
    AccountName = "";
    Balance = 0.0;
}

BankAccount::BankAccount(int numberIn, string nameIn)
{
    AccountNumber = numberIn;
    AccountName = nameIn;
    Balance = 0.0;
}


BankAccount::~BankAccount(void)
{
	// nothing do to
}

int BankAccount::getAccountNumber(void)
{
return AccountNumber;
}

string BankAccount::getAccountName(void)
{
return AccountName;
}

double BankAccount::getBalance(void)
{	
return Balance;
}

void BankAccount::deposit(double amountIn)
{
	Balance = Balance + amountIn;
}

void BankAccount::withdraw(double amountIn)
{
	Balance = Balance - amountIn;
}

void BankAccount::setAccountNumber(int numberIn)
{
	AccountNumber=numberIn;
}

void BankAccount::setAccountName(string nameIn)
{
	AccountName=nameIn;
}
void ios::in | ios::out



BankAccount Header

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#pragma once
#include "string"
using namespace std; // don't worry about namespaces for now

class BankAccount
{
	// The attributes for this class
private:
	// private attributes cannot be accessed directly. 
	int AccountNumber;
	string AccountName;
	double Balance;
	
// The methods for this class
public:
	// constructors (overloaded)
	BankAccount(int numberIn, string nameIn);
	BankAccount(void);

	// destructor
	~BankAccount(void);

	// accessor methods (getters)
	int getAccountNumber(void);
	string getAccountName(void);
	double getBalance(void);

	// mutator methods (setters)
	void deposit(double amountIn);
	void withdraw(double amountIn);
	void setAccountNumber(int numberIn);
	void setAccountName(string nameIn);
};
See this?
ifstream myfile ("BankAccount.txt");

You're trying to create an object named myfile. But there already is an object named myfile. Objects can't share names - how would the compiler know which one you meant when you tried to use it?
Topic archived. No new replies allowed.