1 Replies - 113 Views - Last Post: 02 August 2012 - 08:16 PM Rate Topic: -----

#1 trilly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-July 12

helping with ofstream

Posted 02 August 2012 - 06:36 PM

I think I have the general idea for saving something to a txt file. Right now, I am getting the program to run through and it is saving it to a file, but when I go on my computer to open the file it has a bunch of entries that I didn't input and the final one is the accurate one.

This is the part of my code writing to the file:

	cout << "Would you like to save this checkbook to a file? (Y/N): ";
	cin >> save;

	if (toupper(save)=='Y') 
	{
		fout.open("balance.txt", ios::app);
		fout << "***************************************************************"<< endl;
		fout << "********* C H E C K B O O K  B A L A N C E S H E E T **********"<< endl;
		fout << "***************************************************************"<< endl;
		fout << endl;
		fout << endl;
		fout << setw(10) << "Deposits" << setw(20) << "Withdrawals" << setw(30) << "Balance"<< endl;
		fout << "***************************************************************"<< endl;
		fout << setw(60) << currentBal<< endl;
		for (i=0; i<with; i++)
		{
		currentBal = currentBal - wAmt[i];
		fout << setw(20) << "$" << wAmt[i] << setw(35) << currentBal << endl;
		}//end for	
		for (j=0; j<dep; j++)
		{
		currentBal = currentBal + dAmt[j];
		fout << "  $"<<  dAmt[j] << setw(52) << currentBal << endl;
		}//end for
		fout << "***************************************************************"<< endl;
		fout << "***************************************************************"<< endl;
		fout << endl;
		fout << endl;

		fout.close();

		cout << "Would you like to balance another checkbook? (Y/N) :";
		cin >> another;
	}

	else if (toupper(save)=='N')
	{
	cout << "Would you like to balance another checkbook? (Y/N) :";
	cin >> another;
	}




When I input for example, that the starting balance is 200, one withdrawal of 100 and one deposit of 100 so the final balance is 200, this is what the balance.txt file shows:

Quote

***************************************************************
********* C H E C K B O O K B A L A N C E S H E E T **********
***************************************************************


Deposits Withdrawals Balance
***************************************************************
-367220
***************************************************************
***************************************************************


***************************************************************
********* C H E C K B O O K B A L A N C E S H E E T **********
***************************************************************


Deposits Withdrawals Balance
***************************************************************
-367308
***************************************************************
***************************************************************


***************************************************************
********* C H E C K B O O K B A L A N C E S H E E T **********
***************************************************************


Deposits Withdrawals Balance
***************************************************************
200
$100 100
$100 200
***************************************************************
***************************************************************


As you can see, there are a few random checkbook balancesheets and then the final one is the correct one. I'm not sure if there is a problem with how I'm saving it to the file, but I can't seem to figure out why it's like this. I tried to write my code based off of my book/what I could find online.

Is This A Good Question/Topic? 0
  • +

Replies To: helping with ofstream

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 3060
  • View blog
  • Posts: 9,308
  • Joined: 25-December 09

Re: helping with ofstream

Posted 02 August 2012 - 08:16 PM

Since you are opening your file in append mode, any information that is currently in the file will still be there. So if you run your program several times you should have multiple entries.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1