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.
//CheckBook Balance Program
#include <iomanip>
#include <string>
#include <iostream>
#include <fstream>
usingnamespace 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;
}
elseif(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.
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
//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;
}
elseif(printfile =='n')
{
cout << "No file was created."<<endl <<endl;
}