problem with while loop

having an issue with my while loop. the program compiles and runs but only the first line in my data file is being read. any suggestions on how to fix this?

my data file is..

max martin 123 F 20.00
Michael Malloy 56 P 7.50
maRy MiLLs 555 F 17.50
mADdy miLsAp 89 F 6.15

only thing that is printed is: Martin,Max 123 F 20

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
#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>
#include <fstream>
using namespace std;

struct employeetype
{
  string fname, lname;
  int id;
  char status;
  double payrate;
  double hoursworked;
};

void data_out(string, string, int, char, double, double);
string format(string);

int main ()
{
  employeetype employee;

  cout << "Kevin Hunt Assignment #5 Section #1010" "\n\n";

  ifstream infile;
  string filename1;

  cout << "Enter the name of first data file." "\n";
  cin >> filename1;

  infile.open(filename1.c_str());

  cout << "NAME                  ID#     STATUS       RATE" "\n";
  infile >> employee.fname;
    while(infile)
      {
        infile >> employee.lname >> employee.id >> employee.status >> employee.payrate >> employee.hoursworked;
        data_out(employee.fname, employee.lname, employee.id, employee.status, employee.payrate, employee.hoursworked);
        infile >> employee.fname;
      }
return 0;
}

void data_out(string fname, string lname, int id, char status, double payrate, double hoursworked)
{
  cout << format(lname) << ",";
  cout << format(fname);
  cout << right << setw(13) << id;
  cout << right << setw(11) << status;
  cout << right << setw(4) << "$" << right << setw(5) << setprecision(2) << payrate << "\n";
}

string format(string word)
{
  string temp = " ";
  int wlen;
  wlen = word.length();
  temp = temp + char(toupper(word[0]));
  for(int i = 1; i<wlen; i++)
    {
      temp = temp + char(tolower(word[i]));
    }
  return temp;
}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
while (infile >> employee.fname)
	{
		infile >> employee.lname >> employee.id >> employee.status >> employee.payrate >> employee.hoursworked;
		data_out(employee.fname, employee.lname, employee.id, employee.status, employee.payrate, employee.hoursworked);

	}
	infile.close();


And you haven't entered the hours worked in your data file.

Kevin Hunt Assignment #5 Section #1010

Enter the name of first data file.
maddy.txt
NAME ID# STATUS RATE
Martin, Max 123 F $ 20
Malloy, Michael 56 P $ 7.5
Mills, Mary 555 F $ 18
Milsap, Maddy 89 F $ 6.2
Press any key to continue . . .


That's what you get if you do the above. :)
Last edited on
closed account (48T7M4Gy)
Also, it's probably obvious now, but data_out doesn't output the hours worked. That didn't 't cause the error producing the file reading problems though.
Your data file has 5 coloums of data but you try to read 6.
This works for me:
infile >> employee.lname >> employee.id >> employee.status >> employee.payrate;
BTW. Why do you store the input in an employee type, but pass only individual vars to data_out ?
thanks... and I was unsure on how to pass the variables using the struct employee type so this was what i came up with. is there a more logical way to do it?
closed account (48T7M4Gy)
1
2
3
4
5
void data_out(employeeType anEmployee)
{
  cout << anEmployee.fname blah blah ;
  ... blah blah
}


Employee is probably closer to the truth than employeeType :)
Last edited on
Topic archived. No new replies allowed.