943,563 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1498
  • C++ RSS
Oct 29th, 2008
0

appending the end of a file

Expand Post »
I'm trying to append the end of the file treatments.txt but am unsure how to use the while loop correctly!! Any suggestions?? I tried boolean with the whiles aswell but can't seem to get it to work either. Like asking do you want to continue adding treatments and if yes set bool to false and exit while statement.


C++ Syntax (Toggle Plain Text)
  1. void enterTreatments( T * treatArray, int numTreatments)
  2. {
  3. int treatmentNumber = -1;
  4.  
  5. ofstream outfile;
  6. outfile.open("treatments.txt");
  7.  
  8. // while(numTreatments > treatmentNumber)
  9. for (int i = 0; i < numTreatments; i++)
  10. {
  11. cout << "Enter treatment ID" << endl;
  12. cin >> treatArray[i].treatmentID;
  13. outfile << treatArray[i].treatmentID;
  14. cin.get();
  15.  
  16. cout << "Enter treatment Description" << endl;
  17. getline(cin,treatArray[i].treatmentDescription);
  18. outfile << treatArray[i].treatmentDescription;
  19.  
  20. cout << "Enter cost for treatment" << endl;
  21. cin >> treatArray[i].cost;
  22. outfile << treatArray[i].cost;
  23.  
  24. cout << "Enter number of required sessions" << endl;
  25. cin >> treatArray[i].noOfRequiredSessions;
  26. outfile << treatArray[i].noOfRequiredSessions;
  27.  
  28. treatArray[i].costPerSession = treatArray[i].cost/treatArray[i].noOfRequiredSessions;
  29.  
  30. //cout << "Enter cost per session " << endl;
  31. //cin >> treatArray[i].costPerSession;
  32. cout << "Cost for each session is = " ;
  33. cout << treatArray[i].costPerSession << endl;
  34. cin.get();
  35.  
  36. outfile << treatArray[i].costPerSession;
  37. treatmentNumber++;
  38.  
  39. }
  40.  
  41. }
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
Seamus McCarthy is offline Offline
34 posts
since Apr 2008
Oct 29th, 2008
1

Re: appending the end of a file

>Any suggestions??
Open the file with the ios::app mode, then you don't have to worry about it. Every write will be an append.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 29th, 2008
0

Re: appending the end of a file

Ohh get you now. Thanks!!
Reputation Points: 11
Solved Threads: 0
Light Poster
Seamus McCarthy is offline Offline
34 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Functions
Next Thread in C++ Forum Timeline: I`m new in c++...i need some help!Uganda





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC