Ok general prog overview.
-read records from text file called hours.txt
-stores records in vector of datatype emp
-emp is custom class with with constructor of
Emp():fName(""), lName(""), hours(0.00), rate(0.00){;}
so (string, string, double, double)
formatted to (string, double) (concatenate names, and multiple numbers)
so what im doing is:
reading the file using
1 2
|
ifstream readData;
readData.open("hours.txt");
|
and put the fields values into the object Emp. Multiple Emps are stored into a vector. I close the stream readData via
readData.close();
Now i open an output stream with
1 2
|
ofstream writeData;
writeData.open("pay.txt");
|
I concatenate the names multiple the numbers and output to a pay.txt file. close file with. FYI ALL THIS WORKS FILE
writeData.close();
Now i reopen the hours.txt to append data. With
writeData.open("hours.txt", ios::app);
(hours.txt instead of pay.txt) write data to it....close it
writeData.close();
now i repeat the first part opening hours.txt for input and the appended info dosnt come up. However when I exit and repeat or go look directly at the file its there. Its like its read hours.txt from a buffer it had, not the newly appended one. code below simplfied. FYI exit to initial while loop not coded :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
int main()
{
vector<Emp> vec; //vector to hold employee information
Emp temp; //temp emp object to gp into vector
ifstream readData; //stream for reading files
ofstream writeData; //stream for writing to files
int i = 0; //counter for loop and vector elements
double tempDbl; //temp double for reading data
double totalPay = 0.00; //total dollar paid out to employees
bool done = 0; //value to indicate program run complete
string response; //variable for users responses
string tempStr; //temp string for reading data
//users continues using program
while (!done)
{
//users choice to add employee or display and write report
cout << endl << "Display report or add employee? (RPT/EMP): ";
cin >> response;
//----------DISPLAY AND WRITE REPORT----------//
if (response == "RPT")
{
//open file to read data from
readData.open("hours.txt");
//if opening file failed
if (readData.fail())
{
...
}
else
{
//read data from file store into vector
while (!readData.eof())
{
readData >> tempStr;
temp.SetFirst(tempStr);
readData >> tempStr;
temp.SetLast(tempStr);
readData >> tempDbl;
temp.SetHours(tempDbl);
readData >> tempDbl;
temp.SetRate(tempDbl);
vec.push_back(temp);
i++;
}
}
//close file
readData.close();
//open file to write to
writeData.open("pay.txt");
i = 0;
//write formated data to file and console
while (i < vec.size())
{
writeData << vec[i].GetName() << " $" << vec[i].GetPay() << endl;
totalPay += vec[i].GetPay();
cout << vec[i].GetName() << " $" << vec[i].GetPay() << endl;
i++;
}
//close file
writeData.close();
}
else if (response == "EMP")
{
cout << endl << "Enter employee data in the format of: "
<< endl << "FIRST LAST HOURS RATE" << endl;
writeData.open("hours.txt", ios::app);
cin.ignore();
getline(cin, tempStr);
writeData << endl << tempStr;
writeData.close();
}
}
cout << endl << endl;
system("pause");
return 0;
|
If the class Emp is required ill post it.