reading from a file and storing data into a struct

I am trying to read data from a file and print it out the screen. I am trying to store the data that is read in to a struct. I feel like I am using either the wrong loop or I am not incrementing i the correct way. The data file is set up like so:

Zachary Law
1000
50
50
Bryan Law
1001
30
50
Lauren Keck
1002
20
50

When I run the program below I am getting the following output to my screen:

Zachary Law
1000
50
50
// This line being skipped
0
0
0

:S

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

#include <iostream>
using namespace std;
#include <string>


struct 
{
	int IDNum;
	string name;
	double wage, hWorked;
}emp[100];

int main()
{
	ifstream infile;
	ofstream ofile;

	infile.open("employees.txt");

	int i = 0;

	while (infile.good())
	{
	
		getline(infile, emp[i].name);
		infile >> emp[i].IDNum;
		infile >> emp[i].wage;
		infile >> emp[i].hWorked;

		cout << emp[i].name << endl;
		cout << emp[i].IDNum << endl;
		cout << emp[i].wage << endl;
		cout << emp[i].hWorked << endl;
		i++;
         }

system("pause")
return 0;
}
Last edited on
Thank you for the reply Miini, I had actually tried this earlier but accidentally used infile.ignore() since I was working from a file :p thanks I will test this out when I get home with cin.ignore()
Just an update to anyone struggling with this, infile.ignore() nor cin.ignore() solved my issue. I ended up just reading the name in separate
infile >> Fname;
infile >> Lname;
Why do everyone use ignore when article clearly advises to use std::ws unless you are required to use ignore?

You most certainly used ignore wrong here as there is no reason why it should fail if done correctly.
No need to get testy. I personally had never even heard of std::ws before reading that article so thank you for introducing it to me.
Sorry for that. It just a thorn in my side: no one ever in my memory used the way recommended in article. Everyone just use first introduced.

When you try something an it does not work, show what you did. You might make a mistake (if you want to use infile.ignore, you need to place it after line 34, for example)
Topic archived. No new replies allowed.