Printing Calculated Output to Textfile

I have a program that balances a checkbook using user input data. I need to print the data from the textbook to a textfile, but I have no idea how to pull all my data and print it. The output on the textfile needs to look exactly the same as the output in the regular window. I don't want any new input when it prints. It just needs to print all the data and keep the formating.
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//CheckBook Balance Program
#include <iomanip>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
//Declare Variables
	float balance;
	int deposits;
	int withdraws;
	float deposit_Array[999];
	float withdraws_Array[999];
	char runAgain;
	float newBalance = 0;
	char printfile;
	float with;
	float dep;
	//Reset values once program restarts
deposits = 0;
withdraws = 0;
balance = 0;

do
{//Begin Runagain Loop

//Give intstructions /Get data
	cout << "PLEASE PAY ATTENTION TO THE FOLLOWING INSTRUCTIONS." <<endl;
	cout << endl;
	cout << "This program is going to help you balance your checkbook."<<endl;
	cout << endl;
	cout << "Please enter your starting balance: $";
	cin >> balance;
	cout << "How many deposits have you made in the past month : ";
	cin >> deposits;

//start counter 
int counter = 1;


//Store deposits in an array 
	for(int ct = 0; ct< deposits; ct++)
	{
		cout << "Please enter deposit number "<<ct+1 << " : $";
		cin >> deposit_Array[ct];
	}

	cout << endl << endl;

//Get withdraw information
	cout << "How many withdraws did you have in the past month : ";
	cin >> withdraws;

//Store withdraws in an array
	for (int ctt = 0; ctt < withdraws; ctt++)
	{
		cout << "Please enter in withdraw number " << ctt+1<< " : $";
		cin >> withdraws_Array[ctt];
	}

//Move to next screen
	system("cls");

//DISPLAY CHECKBOOK SCREEN
cout << "#########################################################################" <<endl;
cout << "##################   Y O U R  C H E C K B O O K  H A S   ################" <<endl;
cout << "##################         B E E N  B A L A N C E D      ################" <<endl;
cout << "#########################################################################" <<endl;
cout << endl << endl;
cout << "       DEPOSITS               WITHDRAWLS                  BALANCE        "<<endl;
cout << "#########################################################################" <<endl;
cout <<    setiosflags(ios::fixed) << setprecision(2);
cout << setw(65)<<balance << endl;
//Show deposits and caluclate balance
for (int i=0; i < deposits; i++)
{
	newBalance= (deposit_Array[i] + balance);
	balance = newBalance;
	cout << setw(15) << deposit_Array[i] <<setw(50) <<newBalance << endl;
}

//Show Withdraws and calculate balance
for (int o=0; o <withdraws; o++)
{
	newBalance =(balance - withdraws_Array[o]);
	balance = newBalance;
	cout << setw(40) << withdraws_Array[o] << setw(25) <<newBalance<< endl;
}


//Formatting
cout << "#########################################################################" <<endl;
cout << "#########################################################################" <<endl;

//Make a copy of checkbook for file

cout <<"Do you want a copy of the check book balance sheet on file? (Y/N) :";
cin >> printfile;
printfile = tolower(printfile);

if (printfile =='y')
{
	ofstream outFile;
	outFile.open("checkbook.txt");
	cout <<"The file has been created."<<endl<<endl;

}

else if(printfile =='n') 
{
	cout << "No file was created."<<endl <<endl;
}

	
	
	//Run again input
cout << "Do you want to run this program again? (Y/N):" << endl;
cin >> runAgain;
runAgain = tolower(runAgain);


system("cls");
}// End loop
while (runAgain == 'y');


	return 0;
}//End Main Function 

I know i need to use a void function for the printing and call it in the check to printfile but i dont know the code necessary to pull all my input. I've come up with this.

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
void checkbookfile(string nameOfFile)//writes the checkbook records to a file
{


	//Create file object and open it
	ofstream outFile;
	outFile.open("checkbook.txt", ios::out);

	//determine whether the file was opened
	if (outFile.is_open())
	{
	{//Print all the output to this file

	}

	//close the file
	outFile.close();

}
	else
		cout << "File could not be opened."<<endl;
	//end if


Actually just realized this could all be accomplished using out data...

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
//Make a copy of checkbook for file

cout <<"Do you want a copy of the check book balance sheet on file? (Y/N) :";
cin >> printfile;
printfile = tolower(printfile);

if (printfile =='y')
{

ofstream out_data("checkbook.txt");

//print data to file
out_data << "#########################################################################" <<endl;
out_data << "##################   Y O U R  C H E C K B O O K  H A S   ################" <<endl;
out_data << "##################         B E E N  B A L A N C E D      ################" <<endl;
out_data << "#########################################################################" <<endl;
out_data<< endl << endl;
out_data<< "       DEPOSITS               WITHDRAWLS                  BALANCE        "<<endl;
out_data<< "#########################################################################" <<endl;
out_data<<    setiosflags(ios::fixed) << setprecision(2);
out_data<< setw(65)<<balance << endl;
//Show deposits and caluclate balance
for (int i=0; i < deposits; i++)
{
	newBalance= (deposit_Array[i] + balance);
	balance = newBalance;
	out_data << setw(15) << deposit_Array[i] <<setw(50) <<newBalance << endl;
}

//Show Withdraws and calculate balance
for (int o=0; o <withdraws; o++)
{
	newBalance =(balance - withdraws_Array[o]);
	balance = newBalance;
	out_data << setw(40) << withdraws_Array[o] << setw(25) <<newBalance<< endl;
}


//Formatting
out_data << "#########################################################################" <<endl;
out_data << "#########################################################################" <<endl;


}

else if(printfile =='n') 
{
	cout << "No file was created."<<endl <<endl;
}

	
Topic archived. No new replies allowed.