vendredi 4 décembre 2015

Function will not read full input

I call a function like so

getseconddata (list2,n)

input file reads

45 P 19
11 S 56
45 S F
30 P F

and the function code reads

void getseconddata(employeetype list2[], int n)
{
ifstream infile2;
  string filename;
  int id, changenum;
  char stat, changealpha;
cout<<"Enter name of second data file"<<endl;
  cin>>filename;
  infile2.open(filename.c_str());
  infile2>>id;
  while (getline(infile2))
    {
infile2>>stat;
      if (stat=='S')
        {
        infile2>>changealpha;
        }
      else if (stat=='P')
        {
    infile2>>changenum;
        }
      infile2>>id;
    }
  infile2.close();
  for (int i=0; i<n; i++)
    {
  cout<<id<<stat<<changealpha<<changenum<<endl;
}
}

output reads 45 P 19 45 P 19 45 P 19 45 P 19

I'm a beginner and have tried rewriting the code and looking up basic functions and eof online.Please help

Aucun commentaire:

Enregistrer un commentaire