IFSTREAM file not outputting anything?

Why is this code not outputting anything? I tried doing

 
  cout << payroll_size[i]

but it was giving me errors. How do I fix this? Please help

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
  /* Project #3 for morning class
  /* 1/25/2018
  */


  #include <iostream>
  #include <fstream>
  #include <sstream>
  #include <string>
  #include <stdlib.h> // needed to use atoi function to convert strings to 
  integer

  using namespace std;

  struct Employees
  {
	string employeeName;
	string employeeID;
	int rate;
	int hours;
  };

  istream& operator >> (istream& is, Employees& payroll)
  {
	char payrollStuff[256];
	int pay = atoi(payrollStuff); // use atoi function to convert string to 
  int

	is.getline(payrollStuff, sizeof(payrollStuff));
	payroll.employeeName = payrollStuff;
	
	is.getline(payrollStuff, sizeof(payrollStuff));
	payroll.employeeID = payrollStuff;
	
	is.getline(payrollStuff, sizeof(payrollStuff));
	payroll.rate = atoi(payrollStuff);
	
	is.getline(payrollStuff, sizeof(payrollStuff));
	payroll.hours = atoi(payrollStuff);
	
	return is;
	
  };

  int main()
  {
	const int SIZE = 5; // declare a constant
	Employees payroll_size[5];
	
	ifstream myFile;
	myFile.open("Project3.txt");
	for (int i=0; i< 5; i++)
	{
		myFile >> payroll_size[i];
	}
	
	
	myFile.close();

	return 0;
  }


I have the .txt file and all the project files in the same folder. Even tried adding the full file location.
closed account (E0p9LyTq)
You are not getting any visible output because you are not printing out anything.

You are opening a file, reading the contents into a struct array, closing the file and then ending the program.

Just as you overrode istream's >> operator, for output you need to override ostream's << operator.
Last edited on
Topic archived. No new replies allowed.