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++;
		
	}

}

Recommended Answers

All 2 Replies

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.