Ok i need to read in 2 files. The first file contains employee data, and second file contains employee ID# and the hours the employee worked. I need to matchup the ID numbers and add the hours, which i have done.

my problem is if there is a invalid ID#, i need to ignore the rest of the line, and count the bad IDs. I am having trouble on where and how to place the code, any help would be appreciated.

im sure the code goes in one of these 2 functions. im thinking code should be something like:

if(list.ID != id)
infile.ignore(100, '\n')
counter++;


int find(employee list[],int n,int id)//search for id#
{
  for (int i=0; i<n; i++)
    if (list[i].ID == id)
        return i;
}

void take_inventory(employee list[],int n)
{
  ifstream infile;
  string filename;
  int id;
  int place;
  double hours, totalHours;
  cout << "Enter Work Data Filename " <<endl;
  cin >> filename;
  infile.open(filename.c_str());
  infile >> id;
  while (!infile.eof())
    {
      infile >> hours;
      place = find(list,n,id); //locate item by id#
      list[place].totalHours=list[place].totalHours+hours;
      infile >> id;
    }
  infile.close();
}

Recommended Answers

All 2 Replies

In your find function, add a line to return -1 after the for loop. This will indicate ID not found/invalid ID. Then in line 23 after you get the value for variable place, add this checking:

if (place == -1)
{
     infile >> id;
     continue;
}

This will skip the addition of total hours and go to the next ID in your work data file.

commented: ty for the help +4

In your find function, add a line to return -1 after the for loop. This will indicate ID not found/invalid ID. Then in line 23 after you get the value for variable place, add this checking:

if (place == -1)
{
     infile >> id;
     continue;
}

This will skip the addition of total hours and go to the next ID in your work data file.

ty dude after spending all night writing the long program i always get stuck on little things. I used what you said to help finish the program. I will paste it to show what i did.

int find(employee list[],int n,int id)//search for id#
{
  int num = 0;
  for (int i=0; i<n; i++){
    if (list[i].ID == id)
        return i;
  }
    return -1;
}
void take_inventory(employee list[],int n)
{
  ifstream infile;
  string filename;
  int id;
  int counter = 0;
  int place;
  double hours, totalHours;
  cout << "Enter Work Data Filename " <<endl;
  cin >> filename;
  infile.open(filename.c_str());
  infile >> id;
  while (!infile.eof())
    {

      place = find(list,n,id); //locate item by id#
           if (place == -1)
     {
       counter++;
       infile.ignore(100,'\n');
     infile >> id;
     continue;
     }
           infile >> hours;
      list[place].totalHours=list[place].totalHours+hours;
      infile >> id;
    }
  cout << "Count " << counter << endl;
  infile.close();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.