Text File

I am practicing and I came across a simple programming exercise from the book I am studying from. The problem states:

Three employees in a company are up for a special pay increase. You are
given a file, say Ch3_Ex7Data.txt, with the following data below: (Last name, First name, Salary, and Pay Increase %)

Miller Andrew 65789.87 5
Green Sheila 75892.56 6
Sethi Amit 74900.50 6.1

The book wants us to output the First name, Last name and Updated Salary in an output file Ch3_Ex7Output.out

I know you can do it the long way like what I have below:

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
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
    ifstream inFile;
    ofstream outFile;

    string firstname, lastname, firstname2, lastname2, firstname3, lastname3;
    double salary, salary2, salary3, payincrease, payincrease2, payincrease3, updatedsalary, updatedsalary2, updatedsalary3;

    inFile.open("Ch3_Ex7Data.txt");
    outFile.open("Ch3_Ex7Output.dat");

    inFile >> lastname >> firstname >> salary >> payincrease;
    inFile >> lastname2 >> firstname2 >> salary2 >> payincrease2;
    inFile >> lastname3 >> firstname3 >> salary3 >> payincrease3;

    updatedsalary = (salary*(payincrease/100)+salary);
    updatedsalary2 = (salary2*(payincrease2/100)+salary2);
    updatedsalary3 = (salary3*(payincrease3/100)+salary3);

    outFile << fixed << showpoint << setprecision(2);
    
    outFile << firstname << " " << lastname << " " << updatedsalary << endl;
    outFile << firstname2 << " " << lastname2 << " " << updatedsalary2 << endl;
    outFile << firstname3 << " " << lastname3 << " " << updatedsalary3 << endl;

    inFile.close();
    outFile.close();

    return 0;

}



If there were more than 3 lines, say 50 lines, how can I read everything in and output it the same way. I started a program below but it only outputs the first line. I know there should be a loop for the output but I am not sure what the parameter/argument will be for the loop.

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
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
    ifstream inFile;
    ofstream outFile;

    string firstname, lastname;
    double salary, payincrease, updatedsalary;

    inFile.open("Ch3_Ex7Data.txt");
    outFile.open("Ch3_Ex7Output.dat");

    while ( !inFile.eof() )
    {
    inFile >> lastname >> firstname >> salary >> payincrease;
    }

    updatedsalary = (salary*(payincrease/100)+salary);
    
    outFile << fixed << showpoint << setprecision(2);
    
    outFile << firstname << " " << lastname << " " << updatedsalary << endl;

    inFile.close();
    outFile.close();

    return 0;

}
The result of reading the data is the loop control:
1
2
while ( inFile >> lastname >> firstname >> salary >> payincrease )
  outFile << lastname << ' ' << firstname << ' ' << ( salary * payincrease / 100 + salary ) << std::endl;
Last edited on
When I compile it with the loop above, the .dat file is empty.
Topic archived. No new replies allowed.