Quiz 4

Problem Statement:

Raphael is again in a bit of a pinch this week. Since his school severely underpays him compared to the standard of living in Tokyo, he decided to start a part-time job at the famous book store chain Kinokuniya. However, since his employers think that he is good at C++, they asked him to gather some statistics from their database. They want to know how many employees work at Kinokuniya who are above 20 years and below 30 years of age (both inclusive).

Their database is stored in a file called database.txt. It consists of lines containing the employee ID (an integer), the employee's last and first names (two strings) (note that the last name comes first), the store ID (an integer which tells you which Kinokuniya the employee works at), the designation (a string), the age (an integer), and the shift (a string which is either "Morning" or "Afternoon" or "Both").

Your task is to help Raphael write a program that reads the employee details from database.txt, stores them in a structure, and then simply cout's the number of employees in the age range 20-30, and also cout's the total number of employees.

Sample Input: (database.txt)

7372 Watabe Isamu 301 Worker 27 Afternoon
7374 Nakanoi Hokusai 301 Accountant 28 Afternoon
7375 Tsutomu Mayu 301 Accountant 25 Morning
7377 Takaoka Akuro 301 Manager 35 Both
7401 Hirano Ise 302 Accountant 31 Morning

Sample Output:

The number of employees in 20-30 age range: 3
The total number of employees: 5

Solution

This quiz is more about the implementation of structures. Note that the problem explicitly asks you to use a structure, so you're better off declaring a structure, and reading in the employee details in a structure. The simplest way of implementing this that I can think of is as follows:
#include <iostream>
#include <string>

using namespace std;

typedef struct employee
{
	int employeeID, storeID, age;
	string lastname, firstname, designation, shift;
}Employee;

int main()
{
	Employee temp;
	int countEmployees = 0, countEmployeesInRange = 0;
	ifstream fin("database.txt", ios::in);

	while(!fin.eof())
	{
		countEmployees++;
		fin>>temp.employeeID>>temp.lastname>>temp.firstname>>temp.storeID>>temp.designation>>temp.age>>temp.shift;
		if(temp.age <= 30 && temp.age >=20)
			countEmployeesInRange++;
	}
	fin.close();

	cout<<"The number of employees in 20-30 age range: "<<countEmployeesInRange<<endl;
	cout<<"The total number of employees: "<<countEmployees<<endl;
	
	return 0;
}