Last piece of data is copied

Hello, after finally deciding to change the code around a bit from my previous forum post, I got it to work. The code, however, copies the last piece of data within my struct when it shouldn't. Here's what I got:

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
65
66
67
68
69
70
void Open()
{
	string rfile;
	string temp;
	
	cout << "Enter a file name to read from (be sure to add extension, \".txt\" to the end and use no spaces): ";
	cin >> rfile;
	
	if(fexists(rfile.c_str()))
	{
		ifstream infile;
		infile.open(rfile.c_str());
		int ID, hpw;
		double salary;
		
		while(true)
		{
			if(infile.eof())
			{
				break;
			}
			getline(infile, temp, ';');
			// istringstream buffer1(temp);
			// buffer1 >> ID;
			istringstream(temp) >> ID;
			a.ID = ID;
			getline(infile, temp, ';');
			a.fname = temp;
			getline(infile, temp, ';');
			a.lname = temp;
			getline(infile, temp, ';');
			// istringstream buffer2(temp);
			// buffer2 >> salary;
			istringstream(temp) >> salary;
			a.salary = salary;
			getline(infile, temp, ';');
			// istringstream buffer3(temp);
			// buffer3 >> hpw;
			istringstream(temp) >> hpw;
			a.hpw = hpw;
			getline(infile, temp, '\n');
			a.work = temp;
			ListEmployee.push_back(a);
                        /*
                           Kept getting a "stoi is not a member of std"
                        */
			// getline(infile, temp, ';');
			// a.ID = std::stoi(temp);
			// getline(infile, temp, ';');
			// a.fname = temp;
			// getline(infile, temp, ';');
			// a.lname = temp;
			// getline(infile, temp, ';');
			// a.salary = std::stod(temp);
			// getline(infile, temp, ';');
			// a.hpw = std::stoi(temp);
			// getline(infile, temp, '\n');
			// a.work = temp;
			// ListEmployee.push_back(a);
		}
		
		cout << "Data has been read." << endl;
		infile.close();
		return;
	}
	else
	{
		cout << "File does not exist.  No data read from any file." << endl;
	}
}


Is there anyway for it to stop doing this?...


ID: 1
First name: K
Last name: G
Salary per hour: 20
Hours per week: 40
Working position: m

ID: 2
First name: B
Last name: S
Salary per hour: 10
Hours per week: 50
Working position: e

ID: 3
First name: M
Last name: S
Salary per hour: 30
Hours per week: 30
Working position: m

ID: 3
First name:
Last name:
Salary per hour: 30
Hours per week: 30
Working position:



Having that fourth piece of info in there like that? Any help with that would be appreciated.
If you're looping on eof(), don't; that only works after you've already tried to read and failed from the file, resulting in a garbage entry. Try looping on good() instead.
so, while(infile.good())?
I just tried that, still get the same output. Should I add ListEmployee.pop_back()?
What does you file look like? Does it have any trailing whitespace or anything at the end?
The file would look like this:

ID;fname;lname;salary;hpw;work
ID;fname;lname;salary;hpw;work
.
.
.
and at the end would probably be an empty line

would that empty line be the reason?
Last edited on
Yup, that newline was the reason why it was doing that. Thanks for the help! =D
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
void Open()
{
    string rfile;

    cout << "Enter a file name to read from (be sure to add extension, \".txt\" to the end and use no spaces): ";
    cin >> rfile;

    std::ifstream infile(rfile.c_str()) ;

    bool extractionWasSuccessful = infile ;
    if ( extractionWasSuccessful )
    {
        while ( extractionWasSuccessful )
        {
            std::string temp ;
            getline(infile, temp, ';') ;
            std::istringstream(temp) >> a.ID ;

            std::getline(infile, a.fname, ';') ;
            std::getline(infile, a.lname, ';') ;

            std::getline(infile, temp, ';') ;
            std::istringstream(temp) >> a.salary ;

            std::getline(infile, temp, ';') ;
            std::istringstream(temp) >> a.hpw ;

            std::getline(infile, a.work, '\n') ;

            extractionWasSuccessful = infile ;

            if ( extractionWasSuccessful )
                ListEmployee.push_back(a) ;
        }

        cout << "Data has been read." << endl;
    }
    else
        cout << "File does not exist.  No data read from any file." << endl;
}
My Save() function was leaving a newline at the end of the files, which was causing the Open() function to copy the same data at the end twice.
Topic archived. No new replies allowed.