Help with text file

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
62
63
64
65
#include <iostream>
#include<string>
#include<fstream>

using namespace std;
int createAccount(string customerName, int checkingBalance, int savingsBalance);
int main() {
	int checkingBalance = 0;
	int savingsBalance = 0;
	string customerName;

	cout << "Welcome!" << endl << "Enter your name: ";
	cin >> customerName;
	createAccount(customerName,checkingBalance,savingsBalance);
	return 0;
}

int createAccount(string customerName,int checkingBalance, int savingsBalance){
	char infoChoice = 'x';
	char checkingChoice, savingsChoice;
	int checkingDeposit, checkingWithdrawl, savingsDeposit, savingsWithdrawl;
	string historyFileName = customerName + " History";
	ofstream customerHistoryFile(historyFileName.c_str());
	
        while(infoChoice != 'e'){
	cout << "Account created!" << endl << "if you would like to view     your   checking account press c, for savings press s, and for a transaction history   press h, press e to exit" << endl;
		cin >> infoChoice;
		if(infoChoice == 'c'){
			cout << "Would you like to deposit, withdraw, or view your balance?" << endl << "d to deposit, w to withdraw, and b for balance.";
			cin >> checkingChoice;
			if(checkingChoice == 'd'){
				cout << "How much would you like to deposit?" << endl;
				cin >> checkingDeposit;
				checkingBalance = checkingBalance + checkingDeposit;
				customerHistoryFile.open(historyFileName.c_str(), ios::ate | ios::in);
				customerHistoryFile << "You deposited: " << checkingDeposit << " dollar(s).";
				customerHistoryFile.close();
				
			}
		
			else if(checkingChoice == 'w'){
				cout << "How much would you like to withdraw?" << endl;
				cin >> checkingWithdrawl;
				checkingBalance = checkingBalance - checkingWithdrawl;
				customerHistoryFile.open(historyFileName.c_str(), ios::ate| ios::in);
				if(customerHistoryFile.is_open()){
					customerHistoryFile << "You withdrew: " << checkingWithdrawl << " dollar(s)" << endl;
				}
				customerHistoryFile.close();
				
			}
			
			else if(checkingChoice == 'b'){
				cout << checkingBalance;
			}
		}
		
	}
}
	
	
	
	



If I go through and get to the point where I deposit money in to the checking account, and then go to folder where the text file is stored and open the text file, it is blank.

To clarify, the program runs to completion.
Last edited on
Run your code once more and deposit some arbitrary number into your bank account. Without terminated your application, deposit another arbitrary amount of money into your bank account. The text file should now contain information on the latest deposit. I'm going to assume this transpires because the stream is opened twice and closed once on your first attempt to deposit. When you deposit money into your bank account the second time, the stream is only opened once. I don't know exactly why that mistake would cause that behavior, but it does.

1
2
ofstream customerHistoryFile(historyFileName.c_str());
ofstream customerHistoryFile;

Rather than calling the constructor that takes a character pointer as an argument, you should call the default constructor of std::basic_ofstream. This should fix the problem.

That's one problem solved, but there are two others that I have noticed in your source code.
Last edited on
Thank you, I appreciate the response. I tried what you said and it did not work(about running the application and checking the text file before I close it), but I do not quite understand what you mean about using basic_ofstream as opposed to what I am currently using.

Also, I'd be very interested to know what the other two problems in my source code are, if you'd care to tell, as I'm very new to programming if that wasn't obvious.
customerHistoryFile.open(historyFileName.c_str(), ios::ate| ios::in);

You want to output to the file:
customerHistoryFile.open(historyFileName.c_str(), ios::ate| ios::out);
I thought that would work, but unfortunately it did not. Thank you for the reply though.
Topic archived. No new replies allowed.