Help with input and output

For a Beginner C++ course, I have this assignment:

Write a program that will read the payroll file. It should output the name and paycheck amount to the console. Also output the total pay of all employees to a data file "totals.dat". The input file contains the name followed by hourly wage followed by hours worked.

I was given a link to download this file as input: https://alamo.instructure.com/courses/933520/files/53655501/download

This is the code I have so far, I have not yet added the output commands.

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


using namespace std;

int main()
{
	ifstream data_in;
	ofstream data_out;
	string name = "";
	int hourlyWage = 0, hoursWorked = 0;
	int count = 0;
	data_in.open("payroll.dat");
	data_in>>name;
	double paycheck;
	bool done;
	if(!(data_in.eof()))
	{
		data_in>>name;
		data_in>>hourlyWage;
		data_in>>hoursWorked;
		done = false;
	}
	else done = true;
	while(done==false)
	{
		paycheck = (hoursWorked = hourlyWage);
		cout<<name<<"		"<<paycheck<<endl;
		count++;
		data_in>>name;
		if(!(data_in.eof()))
		{
			data_in>>name;
			data_in>>hourlyWage;
			data_in>>hoursWorked;
			done = false;
		}
		else
		done = true;
	}
	return 0;
}


When I run the code, it outputs "10.50 40" infinitely. What did I do wrong?
Can you post the text file another way?? It cannot be read with the link you provided.
The link requires username and password to access.
This is not a good idea: if(!(data_in.eof())) { try_to_read_something ; process_what_was_read ; }

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

// using namespace std;

int main()
{
    const char* const path_to_datafile = "payroll.dat" ;

    // create file payroll.dat //// you won't need this ///////////////
    { std::ofstream(path_to_datafile) << "Athos 20.5 7\n" "Aramis 46.8 32\n" "Porthos 25 19\n" ; }
    ////////////// you won't need this ///////////////

    // open the file for input
    std::ifstream data_in(path_to_datafile);

    std::string name ;
    double hourly_wage ;
    int hours_worked ;
    int record_count = 0 ;

    std::cout << std::fixed << std::setprecision(2) ;
    const int NAME_FLD_WIDTH = 8 ; // adjust these as required
    const int WAGE_FLD_WIDTH = 6 ;
    const int HOURS_FLD_WIDTH = 4 ;
    const int PAYCHECK_FLD_WIDTH = 9 ;

    // read data from file 3-tuple by 3-tuple till eof
    // canonical C++ i/o loop: check for input failure *after* attempted input
    // breaks out of the loop when input fails 
    // ie. bool( result_of_the_attempted_input_operation ) is evauated as false
    while( data_in >> name >> hourly_wage >> hours_worked )
    {
        ++record_count ;
        const double paycheck = hourly_wage * hours_worked ;
        std::cout << record_count << '.'
                  << std::setw(NAME_FLD_WIDTH) << name
                  << std::setw(WAGE_FLD_WIDTH) << hourly_wage
                  << std::setw(HOURS_FLD_WIDTH) << hours_worked
                  << std::setw(PAYCHECK_FLD_WIDTH) << paycheck << '\n' ;
    }

    std::cout << '\n' << record_count << " records were read\n" ;
}

http://coliru.stacked-crooked.com/a/c31ec2e89b1d6861
closed account (E0p9LyTq)
Delete the data_in>>name; at lines 16 & 32.

Change hourlyWage to double.

And paycheck = (hoursWorked * hourlyWage);

hourlyWage being an int was your biggest code killer.
Last edited on
FurryGuy, I did as you suggested, but now my output looks like this:

0x488010 0

My bad. The file I downloaded originally was corrupted. I fixed it.
Topic archived. No new replies allowed.