This coding of C++ cannot displays correctly once i run it. Please look for the
ID: 3839303 • Letter: T
Question
This coding of C++ cannot displays correctly once i run it.
Please look for the errors or what need to be added.
Here are 3 files of coding.
//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
//Employee.cpp
#include <iostream>
#include<stdlib.h>
#include<string>
#include<iomanip>
#include"Employee.h"
using namespace std;
Employee::Employee(){}
Employee::~Employee() {}
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");
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[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");
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";
num++; //Number of entries should be increased by whenever a new record is added.
}
void Employee::deletes()
{
system("cls");
Employee emp[20],tempemp[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 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");
}
//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;
}
}
Explanation / Answer
Hi, I have fixed the issue.
####### 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
############ Employee.cpp ###########
#include <iostream>
#include<stdlib.h>
#include<string>
#include<iomanip>
#include"Employee.h"
using namespace std;
Employee::Employee(){}
Employee::~Employee() {}
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");
//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[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");
//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";
num++; //Number of entries should be increased by whenever a new record is added.
}
void Employee::deletes()
{
system("cls");
Employee tempemp[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 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");
}
################
#include<iostream>
using namespace std;
#include "Employee.h"
int 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;
}
return 0;
}
########## Sample run ###########
secondweek
secondweek g++ MainEmployee.cpp Employee.cpp
secondweek ./a.out
Username : q
Password : 1
--------------------
Login Successful
--------------------
sh: cls: command not found
***** Employees Management System 1.0 *****
Press 1---->Built The Employee Table
Press 2---->Display The Employee Table
Press 3---->Add New Entry
Press 4---->Delete An Entry
Press 5---->Search Arecord
Press 6---->Sort The Table
Press 8---------->Quit Program
Select Your Option Please ====> 1
Build The Table
Maximum number of entries ----- > 20
How many do you want ----->2
Enter The Following Items
Name Pravesh
Code 123
Designation SDE
Years of Experience 2
Age 24
Name Mukesh
Code 432
Designation SSDE
Years of Experience 5
Age 27
Going to main menu
sh: pause: command not found
sh: cls: command not found
***** Employees Management System 1.0 *****
Press 1---->Built The Employee Table
Press 2---->Display The Employee Table
Press 3---->Add New Entry
Press 4---->Delete An Entry
Press 5---->Search Arecord
Press 6---->Sort The Table
Press 8---------->Quit Program
Select Your Option Please ====> 2
sh: cls: command not found
********Display The Table********
Name Code Designation Years(EXP) Age
------------------------------------------------------
Pravesh 123 SDE 2 24
Mukesh 432 SSDE 5 27
going to main menu
sh: pause: command not found
sh: cls: command not found
***** Employees Management System 1.0 *****
Press 1---->Built The Employee Table
Press 2---->Display The Employee Table
Press 3---->Add New Entry
Press 4---->Delete An Entry
Press 5---->Search Arecord
Press 6---->Sort The Table
Press 8---------->Quit Program
Select Your Option Please ====> 8
secondweek
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.