0

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.

void enterTreatments( T * treatArray, int numTreatments)
{
		int treatmentNumber = -1;
		
		ofstream outfile;
                outfile.open("treatments.txt");
        
    //    while(numTreatments > treatmentNumber)
	for (int i = 0; i < numTreatments; i++)
	{
	    cout << "Enter treatment ID" << endl;
	    cin  >> treatArray[i].treatmentID;
	    outfile << treatArray[i].treatmentID;
	    cin.get();
	    
		cout << "Enter treatment Description" << endl;
		getline(cin,treatArray[i].treatmentDescription);
		outfile << treatArray[i].treatmentDescription;
		
		cout << "Enter cost for treatment" << endl;
		cin  >> treatArray[i].cost;
		outfile << treatArray[i].cost;
		
		cout << "Enter number of required sessions" << endl;
		cin  >> treatArray[i].noOfRequiredSessions;
		outfile << treatArray[i].noOfRequiredSessions;
		
		treatArray[i].costPerSession = treatArray[i].cost/treatArray[i].noOfRequiredSessions;
		
		//cout << "Enter cost per session " << endl;
		//cin >> treatArray[i].costPerSession;
		cout  << "Cost for each session is = " ;
		cout  << treatArray[i].costPerSession << endl;
		cin.get();
		
		outfile << treatArray[i].costPerSession;
		treatmentNumber++;
		
	}

}
2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by Seamus McCarthy
0

>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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.