There are 3 files here. Cpp and Header file. There is an error because im not de
ID: 3839275 • Letter: T
Question
There are 3 files here.
Cpp and Header file.
There is an error because im not declare the tememp in cpp file. Where should i declare it?
Here the coding
//MainEmployee.cpp
#include<iostream>
using namespace std;
#include "Employee.h"
void main()
{
int option;
Employee emp;
emp.login();
emp.menu();
cout << " Select Your Option Please ====> ";
cin >> option;
while (option != 8)
{
switch (option)
{
case 1:
emp.build();
break;
case 2:
emp.display();
break;
case 3:
emp.add();
break;
case 4:
emp.deletes();
break;
case 5:
emp.search();
break;
case 7:
emp.display();
break;
default:
cout << "invalid selection" << endl;
}
emp.menu();
cout << " Select Your Option Please ====> ";
cin >> option;
}
}
// Employee.cpp
#include <iostream>
#include<string>
#include<iomanip>
#include"Employee.h"
using namespace std;
Employee::Employee(){}
Employee::~Employee() {}
void Employee::login()
{
string pd, password = "1";
string un, username = "q";
bool loginSuccess = false;
int att = 0;
while(att<3)
{
cout << "Username : ";
cin >> un;
cout << "Password : ";
cin >> pd;
if (username == un && password == pd)
{
cout << " -------------------- ";
cout << "Login Successful" << endl;
cout << "-------------------- ";
loginSuccess = true;
break;
}
else
{
cout << " ------------------------ ";
cout << "Invalid ID or PASSWORD !" << endl;
cout <<" Please try again you have maximum 3 attempt only " << endl;
att++;
}
if (att == 3)
{
cout << "You have reach the limit attempt " << endl;
exit(-99);
}
}
}
void Employee::menu()
{
system("cls");
cout << " ";
cout<<(" ***** Employees Management System 1.0 ***** ");
cout << endl;
cout << " ";
cout << " Press 1---->Built The Employee Table ";
cout << " ";
cout << " Press 2---->Display The Employee Table ";
cout << " ";
cout << " Press 3---->Add New Entry ";
cout << " ";
cout << " Press 4---->Delete An Entry ";
cout << " ";
cout << " Press 5---->Search Arecord ";
cout << " ";
cout << " Press 6---->Sort The Table ";
cout << " ";
cout << " Press 8---------->Quit Program ";
cout << " ";
}
void Employee::build()
{
int num;
Employee emp[20];
cout<< " Build The Table"<<endl;
cout << "Maximum number of entries ----- > 20" << endl;
cout << "How many do you want ----->";
cin >> num;
cout << "Enter The Following Items" << endl;
for (int i = 0; i <= num-1; i++)
{
cout << "Name ";
cin >> emp[i].name;
cout << "Code ";
cin >> emp[i].code;
cout << "Designation ";
cin >> emp[i].designation;
cout << "Years of Experience ";
cin >> emp[i].exp;
cout << "Age ";
cin >> emp[i].age;
}
cout << "Going to main menu"<<endl;
system("pause");
}
void Employee::display()
{
system("cls");
Employee emp[20];
cout<<(" ********Display The Table********")<<endl;
cout << " Name Code Designation Years(EXP) Age " << endl;
cout << " ------------------------------------------------------" << endl;
for (int i = 0; i <= num - 1; i++)
{
cout << setw(13) << emp[i].name;
cout << setw(6) << emp[i].code;
cout << setw(15) << emp[i].designation; cout << setw(10) << emp.exp;
cout << setw(15) << emp[i].age;
}
cout << "going to main menu"<<endl;
system("pause");
}
void Employee::add()
{
system("cls");
Employee emp[20];
int i = num;
cout<<("Insert New Record");
cout << endl;
cout << "Enter The Following Items" << endl;
cout << "Name ";
cin >> emp[i].name;
cout << "Code ";
cin >> emp[i].code;
cout << "Designation ";
cin >> emp[i].designation;
cout << "Years of Experience ";
cin >> emp[i].exp;
cout << "Age ";
cin >> emp[i].age;
cout << endl << endl;
cout << "going to main menu";
}
void Employee::deletes()
{
system("cls");
Employee emp[20],tememp[20],sortemp[20],sortemp1[20];
int jobcode;
int check;
cout<<("Delete An Entry");
cout << endl;
cout << "Enter An JobCode To Delete That Entry ";
cin >> jobcode;
int i;
for (i = 0; i <= num - 1; i++)
{
if (emp[i].code == jobcode)
{
check = i;
}
}
for (i = 0; i <= num - 1; i++)
{
if (i == check)
{
continue;
}
else
{
if (i>check)
{
tempemp[i - 1] = emp[i];
}
else
{
tempemp[i] = emp[i];
}
}
}
num--;
for (i = 0; i <= num - 1; i++)
{
emp[i] = tempemp[i];
}
system("pause");
}
void Employee::search()
{
system("cls");
Employee emp[20],tempemp[20],sortemp[20],sortemp1[20];
cout<<("Welcome To Search Of Employee Database ");
cout << endl;
cout << endl;
int jobcode;
cout << "You Can Search Only By Jobcode Of An Employee";
cout << "Enter Code Of An Employee ";
cin >> jobcode;
for (int i = 0; i <= num - 1; i++)
{
if (emp[i].code == jobcode)
{
cout << " Name Code Designation Years(EXP) Age ";
cout << " ------------------------------------------------------ ";
cout << setw(13) << emp[i].name;
cout << setw(6) << emp[i].code;
cout << setw(15) << emp[i].designation;
cout << setw(10) << emp[i].exp;
cout << setw(15) << emp[i].age;
cout << endl;
}
}
cout << "going to main menu";
system ("pause");
}
//Employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
const int max = 20;
class Employee
{
public:
Employee();
~Employee();
void login();
void menu();
void build();
void add();
void deletes();
void search();
void display();
private:
char name[20];
long int code;
char designation[20];
int exp;
int age;
int num;
};
#endif
Explanation / Answer
1. Include this line in Employee.cpp #include <stdlib.h>
2. In Employee::deletes function you have to change the declaration from tememp[20] to tempemp[20]
3.I have done some changes to Employee.cpp file [changes will be in Bold and please find comments for the changes done]
4.Include #include "Employee.cpp" in MainEmployee.cpp.
// Employee.cpp
#include <iostream>
#include <stdlib.h>
#include<string>
#include<iomanip>
#include"Employee.h"
using namespace std;
Employee::Employee(){}
Employee::~Employee() {}
//Employee and num should be declared as global variables for all the functions to access these variables
//I have Removed all the local declarations of emp[20] and num in all the functions[whereever they were declared]
Employee emp[20];
int num;
void Employee::login()
{
string pd, password = "1";
string un, username = "q";
bool loginSuccess = false;
int att = 0;
while(att<3)
{
cout << "Username : ";
cin >> un;
cout << "Password : ";
cin >> pd;
if (username == un && password == pd)
{
cout << " -------------------- ";
cout << "Login Successful" << endl;
cout << "-------------------- ";
loginSuccess = true;
break;
}
else
{
cout << " ------------------------ ";
cout << "Invalid ID or PASSWORD !" << endl;
cout <<" Please try again you have maximum 3 attempt only " << endl;
att++;
}
if (att == 3)
{
cout << "You have reach the limit attempt " << endl;
exit(-99);
}
}
}
void Employee::menu()
{
system("cls");
cout << " ";
cout<<(" ***** Employees Management System 1.0 ***** ");
cout << endl;
cout << " ";
cout << " Press 1---->Built The Employee Table ";
cout << " ";
cout << " Press 2---->Display The Employee Table ";
cout << " ";
cout << " Press 3---->Add New Entry ";
cout << " ";
cout << " Press 4---->Delete An Entry ";
cout << " ";
cout << " Press 5---->Search Arecord ";
cout << " ";
cout << " Press 6---->Sort The Table ";
cout << " ";
cout << " Press 8---------->Quit Program ";
cout << " ";
}
void Employee::build()
{
cout<< " Build The Table"<<endl;
cout << "Maximum number of entries ----- > 20" << endl;
cout << "How many do you want ----->";
cin >> num;
cout << "Enter The Following Items" << endl;
for (int i = 0; i <= num-1; i++)
{
cout << "Name ";
cin >> emp[i].name;
cout << "Code ";
cin >> emp[i].code;
cout << "Designation ";
cin >> emp[i].designation;
cout << "Years of Experience ";
cin >> emp[i].exp;
cout << "Age ";
cin >> emp[i].age;
}
cout << "Going to main menu"<<endl;
system("pause");
}
void Employee::display()
{
system("cls");
cout<<(" ********Display The Table********")<<endl;
cout << " Name Code Designation Years(EXP) Age " << endl;
cout << " ------------------------------------------------------" << endl;
for (int i = 0; i <= num - 1; i++)
{
cout << setw(13) << emp[i].name;
cout << setw(6) << emp[i].code;
cout << setw(15) << emp[i].designation;
cout << setw(10) << emp[i].exp;
cout << setw(15) << emp[i].age;
cout <<endl;//this will show each record in a new line
}
cout << "going to main menu"<<endl;
system("pause");
}
void Employee::add()
{
system("cls");
int i = num;
cout<<("Insert New Record");
cout << endl;
cout << "Enter The Following Items" << endl;
cout << "Name ";
cin >> emp[i].name;
cout << "Code ";
cin >> emp[i].code;
cout << "Designation ";
cin >> emp[i].designation;
cout << "Years of Experience ";
cin >> emp[i].exp;
cout << "Age ";
cin >> emp[i].age;
cout << endl << endl;
cout << "going to main menu";
num++;//Number of Entries should be increased by 1 whenever a new record is added
}
void Employee::deletes()
{
system("cls");
Employee tempemp[20],sortemp[20],sortemp1[20];//declaration was wrong
int jobcode;
int check;
cout<<("Delete An Entry");
cout << endl;
cout << "Enter An JobCode To Delete That Entry ";
cin >> jobcode;
int i;
for (i = 0; i <= num - 1; i++)
{
if (emp[i].code == jobcode)
{
check = i;
}
}
for (i = 0; i <= num - 1; i++)
{
if (i == check)
{
continue;
}
else
{
if (i>check)
{
tempemp[i - 1] = emp[i];
}
else
{
tempemp[i] = emp[i];
}
}
}
num--;
for (i = 0; i <= num - 1; i++)
{
emp[i] = tempemp[i];
}
system("pause");
}
void Employee::search()
{
system("cls");
Employee tempemp[20],sortemp[20],sortemp1[20];
cout<<("Welcome To Search Of Employee Database ");
cout << endl;
cout << endl;
int jobcode;
cout << "You Can Search Only By Jobcode Of An Employee";
cout << "Enter Code Of An Employee ";
cin >> jobcode;
for (int i = 0; i <= num - 1; i++)
{
if (emp[i].code == jobcode)
{
cout << " Name Code Designation Years(EXP) Age ";
cout << " ------------------------------------------------------ ";
cout << setw(13) << emp[i].name;
cout << setw(6) << emp[i].code;
cout << setw(15) << emp[i].designation;
cout << setw(10) << emp[i].exp;
cout << setw(15) << emp[i].age;
cout << endl;
}
}
cout << "going to main menu";
system ("pause");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.