5 Replies - 341 Views - Last Post: 10 December 2012 - 05:27 PM Rate Topic: -----

#1 sensible  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-December 12

Classes/File Program

Posted 06 December 2012 - 04:20 PM

Here's the project description:

New electronic monitors are being installed on all rooms in the building and pass-cards are being issued to each employee in the building. Each time an employee enters or exits a room, the monitor adds the following information to a disk file:

The employee's ID number (4 digit number)
The room number (5 digit number, 2 digits for floor, 3 digits for room)
0 if the employee entered the room, or 1 if he exited the room.

At the end of the work day, the disk file contains a sequence of the above information for every employee's entry or exit to a room, in chronological order.
The building has at most 200 employees. The building has 110 rooms.


write a program on the computer that reads the daily disk file and generates some security reports. The security department is worried about someone forging a pass-card, so your program should print an error message if the same employee appears to be in two or more rooms at once. Also, there is worry that someone might tamper with the room monitors. So your program should print an error message if more than 10 employees appear to be in a room at the same time, or if an emloyee appears to exit a room that he is not in.

Finally, to help the security detectives, your program should produce two reports based on the day's activity. One report lists the names and ID numbers of all the employees who have been in each room. The other report lists all the rooms that have been visited by each employee. These two reports are calculated using data read from the disk file of monitor reports.

Should read three disk files:

A file of the current employees in a plain text disk file named "employee.txt". There are 3 data items about one employee in each record of this file, separated by spaces: an employee's ID number, the employee's first name and the employee's last name. There are never more than 200 employees. An example file with 3 employees is:
:
1001 Jim Smith
1002 John Brown
1004 Eric White

A file of the room numbers in the building is in text file named "rooms.txt". An example file with 3 rooms is:
03241
03242
03244

A file of monitor reports for the day is in a plain text file named "reports.txt". Each line of this file contains a report with 3 data items:

the employee ID
the room ID
0 if enter, or 1 if exit,

An annotated example file with 5 reports is:
1001 03241 0 employee 03241 enters room 1001
1002 03242 0 employee 03242 enters room 1002
1004 04170 0 employee 04170 enters room 1004
1002 03242 1 employee 03242 exits room 1002
1002 01420 0 employee 01420 enters room 1002

Okay so i'll be honest here, i'm pretty overwhelmed by this project. I know using classes will be easier, it's obviously going to use an array, a loop, and ifstream/ofstream in it. I'm just having problems putting it all together. And guide/pseudo code or any code at all would be helpful.

Here's the "lowdown" of what I think the classes might look like (haven't wrote code for the methods yet):
Employee.h Class:

#ifndef EMPLOYEE_H
#define EMPLOYEE_H

class Room;
class Employee
{
public:
	Employee(int ID, char *fname, char *lname, Room);

void readEmployee();
void enter();

private:
	int ID;
	char firstname[20];
	char lastname[20];
	
};
#endif


Employee.cpp file
#include "Employee.h"
#include "Room.h"

Employee::Employee(int id, char *Fname, char *Lname, Room)
{
	ID=id;
	firstname[20]=Fname[20];
	lastname[20]=Lname[20];
}

void enter() //reads the 0 or 1 to determine if entered or not
{
}
void readEmployee() //reads employee id and name
{
}


room.h
#ifndef Room_H
#define Room_H

class Employee;
class Room
{
public:
	Room(int rmNUM);

	void readRoom();

private:
	int roomNumber;
};
#endif


room.cpp
#include "Room.h"
#include "Employee.h"


Room::Room(int rmNum)
{
	roomNumber=rmNum;
}

void readRoom() //reads room number from file
{
}



Sorry for the long read but trying to give as much info as possible. Would like to know if i'm heading in the correct direction.

This post has been edited by JackOfAllTrades: 06 December 2012 - 05:05 PM
Reason for edit:: Split code based on files


Is This A Good Question/Topic? 0
  • +

Replies To: Classes/File Program

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Classes/File Program

Posted 06 December 2012 - 05:07 PM

I'm going to say before you go any further you either learn how to use C-strings or, ideally, the C++ std::string class, because this

firstname[20]=Fname[20];
lastname[20]=Lname[20];



is WAY wrong.

Here's a tutorial for you to study.
Was This Post Helpful? 1
  • +
  • -

#3 sensible  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-December 12

Re: Classes/File Program

Posted 06 December 2012 - 10:54 PM

I have a pretty good idea of how C-Strings work I just wasn't sure how to declare it in the .cpp file since i had already declared it in the header file.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Classes/File Program

Posted 07 December 2012 - 04:49 AM

Quote

I have a pretty good idea of how C-Strings work

I don't think you have the basics down, because by this point you should know:

1. you can't copy arrays that way
2. if you have an array of 20 elements, only indexes 0 through 19 are valid

I would reevaluate your assessment of your understanding.
Was This Post Helpful? 0
  • +
  • -

#5 sensible  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-December 12

Re: Classes/File Program

Posted 10 December 2012 - 04:59 PM

View PostJackOfAllTrades, on 07 December 2012 - 04:49 AM, said:

Quote

I have a pretty good idea of how C-Strings work

I don't think you have the basics down, because by this point you should know:

1. you can't copy arrays that way
2. if you have an array of 20 elements, only indexes 0 through 19 are valid

I would reevaluate your assessment of your understanding.


Alright I just changed it to where i'm just using regular strings instead. I have my program working to where I read in all the files and store them in an array. It compiles and prints out fine. The confusing part is getting it to produce error messages if an employee is in 2 rooms at the same time (two 1's next to the same id number) and an error message if more than 10 employee's is in a room, and an error if an employee attempts to leave a room he isn't in. I have to somehow read and compare information from the files that I have stored in the array's. Here's what I have

main:
// read monitor reports file
#include "Employee.h"
#include "Room.h"
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;


//Employee variables
 int e[200];
 int empid;
 char name[20];
 char str[80];
//Room variables
 int r[110];
 int roomid;
 int n;
//Records variables
 int id;
 int enter_exit;
 void *gotit;

void readReports()
{
//reports
  
  // specify the name of the file to read:  reports.txt
  //ifstream infile("C:\\reports.txt",ios::in);
  ifstream infile("reports.txt");

  if (!infile) {
     cerr<<"Can't find reports.txt file. Bye."<<endl;
    exit(1); 
  }
  // read each monitor reports
  for (n=0; n<400; n++) {  // at most 400 reports.
     // read one more report
     gotit = infile>>id>>roomid>>enter_exit;
     if (!gotit) break;   // there are no more  records

     // got another record, print out the information.
     cout<<id<<" "<<roomid<<" "<<enter_exit<<endl;
     // here save the report data in a "report" object.
  }
  // print out how many reports we read.
  cout<<"Read "<<n<<" reports."<<endl;
}

void readRooms()
{
	int i = 0;

    ifstream inFile("rooms.txt");

	if(!inFile)
	{
		cerr<<"Can't find room.txt file. Bye.";
		exit(1);
	}
	 for (n=0; n<110; n++) {  // at most 110 rooms.
     // read one more room
     gotit = inFile>>roomid;
     if (!gotit) break;   // there are no more rooms

     // got another record, print out the information.
     cout<<roomid<<endl;

	 for(i=0;i<110;i++)
	 {
		 r[i]=roomid;
	 }

  }
  cout<<"Read "<<n<<" rooms."<<endl;
}


void readEmployee()
{
  int i = 0;
  ifstream inFile("employees.txt");

  if(!inFile)
  {
	  cerr<<"Can't find employee.txt file. Bye."<<endl;
	  exit(1);
  }
  while (true)
  {

	  inFile.get(str, 80, ' '); 

	  if (inFile.eof()) break;
	  empid = atoi(str); //convert empid to int
	  inFile.get();

	  inFile.getline(name,20); //read the last line (first and last name)
	  if (inFile.eof()) break;

	  for(i=0; i<200;i++)
	  {
	  e[i]=empid;
	  }
	  cout<<empid<<" "<<name<<endl;
  }

  string name(name); //convery c-string to string
  

}

int main() {

	readEmployee();
	readRooms();
	readReports();
	
	Room rm(roomid);
	Employee emp(empid, name, rm);
	
	system("PAUSE");

}
//Employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include<string>

using namespace std;

class Room;
class Employee
{
public:
	Employee(int ID, string name, Room);

void Eset(string n, int ID);


private:
	int empID;
	string name;
	
};
#endif
Employee.cpp
#include "Employee.h"
#include "Room.h"
#include <iostream>
#include <string>

using namespace std;

Employee::Employee(int id, string n, Room)
{
	name=n;
	empID=id;
}

void Eset(string n, int id)
{
}
//Room.h
#ifndef Room_H
#define Room_H
class Employee;
class Room
{
public:
	Room(int rmNum);

	void rSet(int roomID);

private:
	int roomNumber;
};
#endif
//Room.cpp
#include "Room.h"
#include "Employee.h"
#include<string>
#include<iostream>

using namespace std;

Room::Room(int rmnum)
{
	roomNumber=rmnum;
}

void rSet(int roomID)
{
}



Any help would be greatly appreciated
Was This Post Helpful? 0
  • +
  • -

#6 sensible  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-December 12

Re: Classes/File Program

Posted 10 December 2012 - 05:27 PM

I forgot, here are the 3 files that are being read.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1