I have a program that takes user input and writes it to a text file. However, when I try to append to my text file, it rewrites my "header" line. How can I avoid that?
// StudyTime.cpp :
/* Track study time
accept user input
store in .txt file
*/
#include "stdafx.h"
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
string date = "";
double hours = 0;
const string filename = "StudyTime.txt";
ofstream myfile (filename, ios_base::app); //create filename and allow appending
myfile << "Date" << setw(15) << "Study Time" << endl; //Header line
//prompt user for Date
cout << "Date: ";
cin >> date;
//prompt user for hours
cout << "Hours: ";
cin >> hours;
if (date.size() == 3) {
myfile << date << setw(8) << hours << endl; //write date and hours to txt file
}
else if (date.size() == 4) {
myfile << date << setw(7) << hours << endl; //write date and hours to txt file
}
else
myfile << date << setw(6) << hours << endl; //write date and hours to txt file
myfile.close(); //close file
return 0;
}