(Urgent) I'm having trouble with the (std::!cin.eof()) function.

I'm currently taking an online course involving C++ program but I've never used it (I've only used Choicescript) and I ran into an issue regarding input files. The way this college is set up, I can't really ask for help because pretty much, I won't get it so I'd really appreciate if I could get some help with this before Monday.

#include <iostream>
#include <fstream>

using namespace std;


int main (){
int employee_id;
int hours_worked;
float hourly_rate, gross_pay, tax_rate, net_pay;

std::cin >> employee_id >> hours_worked >> hourly_rate;
while (!cin.eof("payrolliteration4.txt") ){
gross_pay = hours_worked*hourly_rate;
std::cout <<"Employee ID is " << employee_id <<"The grosspay is " << gross_pay << std::endl;
std::cin >> employee_id >> hours_worked >> hourly_rate;
}//WHILE

return 0;
}//MAIN

This is my program. I did it the way the guy wanted me to but it's still not really working.

13 41 C:\Users\IsabelandPhoebe\Documents\Payroll Iteration 4.cpp [Error] no matching function for call to 'std::basic_istream<char>::eof(const char [22])'

this is the associated error message. I don't know why it isn't working considering I did it the way the professor told me to.

Sorry for bothering the worry, in any regard.
!cin.eof("payrolliteration4.txt") eof dosn't take a string. looping on eof is bad programming anyways.

I ran into an issue regarding input files.

Your not using input files. You included fstream but haven't used it.
What should I use instead in that case? I keep asking the guy but all he does is copy paste screenshots of the output of his program which isn't really helpful.

I'm not? Er, I have no idea regarding that. I'm just supposed to do what the guy tells me to but he published the textbooks himself and hasn't revised them so I'm sort of stuck. He's the one that told me to use eof.
There's info on using files in these links that might be helpful.

http://www.cplusplus.com/doc/tutorial/files/
http://www.umich.edu/~eecs381/handouts/filestreams.pdf
Okay so fixed the program somewhat. It now reads the file and doesn't repeat into infinity but the last line of the file registers twice for some reason.

Here's the updated program:

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

using namespace std;

int main (){
	int employee_id;
	int hours_worked;
	float hourly_rate, gross_pay, tax_rate, net_pay;
	
	ifstream myfile ("payroll4.txt");
	//std::cout << "I got past opening the file" <<std::endl;
	if (myfile.is_open())
	{
		//std::cout << "File is open and i'm in front of while statement!" << std::endl;
		//	std::cin > myfile >> employee_id >> hours_worked >> hourly_rate;
	
		while (!(myfile.fail()))
			{
				std::cin > myfile >> employee_id >> hours_worked >> hourly_rate;
			//	counter++;
			//	std::cout << "Number of times cin was executed=" << counter << std::endl;
				gross_pay = hours_worked*hourly_rate;
				std::cout <<"Employee ID is " << employee_id << "; The grosspay is " << gross_pay << std::endl;
			//	std::cout << " eof()=" << myfile.fail() << std::endl;
			//		system("PAUSE");
		}
	}
	else std::cout << "unable to open file!";
	
	return 0;
}//MAIN 

Check for failure immediately after (and not before) the attempted input operation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	if (myfile.is_open())
	{
		//std::cout << "File is open and i'm in front of while statement!" << std::endl;
		//	std::cin > myfile >> employee_id >> hours_worked >> hourly_rate;
	
		// while (!(myfile.fail())) // *******
		while( myfile >> employee_id >> hours_worked >> hourly_rate ) // *********
                {
		        // std::cin > myfile >> employee_id >> hours_worked >> hourly_rate; // ********
			//	counter++;
			//	std::cout << "Number of times cin was executed=" << counter << std::endl;
				gross_pay = hours_worked*hourly_rate;
				std::cout <<"Employee ID is " << employee_id << "; The grosspay is " << gross_pay << std::endl;
			//	std::cout << " eof()=" << myfile.fail() << std::endl;
			//		system("PAUSE");
		}
	}
	else std::cout << "unable to open file!";
Okay now it's working. Thank you very much.
Topic archived. No new replies allowed.