In this c++ coding, I put 20 as the limit. But it seems like did not function, i
ID: 3841165 • Letter: I
Question
In this c++ coding, I put 20 as the limit. But it seems like did not function, if I put 20 input, it still display
.
Please fix my errors so it will display " exceed the limit" in coding.
******Employee.cpp*****
#include <iostream>
#include<string>
#include<iomanip>
#include"Employee.h"
using namespace std;
Employee::Employee(){}
Employee::~Employee() {}
Employee emp[20], tempemp[20], sortemp[20], sortemp1[20],temp[20];
int num, i, j,jobcode;
bool sorted;
void Employee::login()
{
system("COLOR 0A");
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 and this program will exit " << endl;
exit(-99);
}
}
system("pause");
}
void Employee::menu()
{
system("cls");
cout << " ";
cout<<(" ***** Employees Management System 1.0 ***** ")<<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---->Edit An Entry ";
cout << " ";
cout << " Press 6---->Search Job Code ";
cout << " ";
cout << " Press 7---->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<<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(7) << emp[i].name;
cout << setw(9) << emp[i].code;
cout << setw(12) << emp[i].designation;
cout << setw(15) << emp[i].exp;
cout << setw(14) << emp[i].age;
cout << endl;
}
cout << " Going to main menu"<<endl<<endl;
system("pause");
}
void Employee::add()
{
system("cls");
int i = num;
num += 1;
cout<<("Insert New Record")<<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;
cout << "Going to main menu" << endl<<endl;
system("pause");
}
void Employee::deletes()
{
system("cls");
int code;
int check;
cout<<("Delete An Entry");
cout << endl;
cout << "Enter An Job Code Number To Delete Entry : ";
cin >> code;
int i;
for (i = 0; i <= num - 1; i++)
{
if (emp[i].code == code)
{
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];
}
cout << " Press Any Key To Go Back" << endl<<endl;
system("pause");
}
void Employee::edit( )
{
system("cls");
cout << "Enter Job Code Number To Edit Of An Entry : ";
cin >> jobcode;
for (int i = 0; i <= num - 1; i++)
{
if (emp[i].code == jobcode)
{
cout << " Name Code Designation Years(EXP) Age " << endl;
cout << " -----------------------------------------------------" << endl;
cout << setw(7) << emp[i].name;
cout << setw(9) << emp[i].code;
cout << setw(12) << emp[i].designation;
cout << setw(15) << emp[i].exp;
cout << setw(14) << emp[i].age;
cout << endl;
cout << "Enter New Job Code-----> ";
cin >> emp[i].code;
}
}
cout << "Press Any Key To Go Back" << endl << endl;
system("pause");
}
void Employee::search()
{
system("cls");
cout<<("Welcome To Search Of Employee Database ")<<endl<<endl;
cout << "You Can Search Only By Job Code Of An Entry"<<endl<<endl;
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 "<<endl;
cout << " -----------------------------------------------------"<<endl;
cout << setw(7) << emp[i].name;
cout << setw(9) << emp[i].code;
cout << setw(12) << emp[i].designation;
cout << setw(15) << emp[i].exp;
cout << setw(14) << emp[i].age;
cout << endl;
}
}
cout << " Going to main menu"<<endl<<endl;
system("pause");
}
void Employee::sort()
{
system("cls");
cout << ("Sort The Database By Job Code Of An Entry")<<endl<<endl;
for (i = 0; i <= num - 1; i++)
{
sortemp1[i] = emp[i];
}
for (i = 0; i <= num - 1; i++)
{
for (j = 0; j <= num - 1; j++)
{
if (sortemp1[i].code<sortemp1[j].code)
{
temp[i] = sortemp1[i];
sortemp1[i] = sortemp1[j];
sortemp1[j] = temp[i];
}
}
}
for (i = 0; i <= num - 1; i++)
{
cout << " Name Code Designation Years(EXP) Age "<<endl;
cout << " -----------------------------------------------------" << endl;
for (i = 0; i <= num - 1; i++)
{
cout << setw(7) << sortemp1[i].name;
cout << setw(9) << sortemp1[i].code;
cout << setw(12) << sortemp1[i].designation;
cout << setw(15) << sortemp1[i].exp;
cout << setw(14) << sortemp1[i].age<<endl;
}
cout << "Press Any Key To Go Back"<<endl<<endl;
system("pause");
}
}
*****MainEmployee.cpp****
#include<iostream>
using namespace std;
#include "Employee.h"
void main()
{
int option;
Employee m;
m.login();
m.menu();
cout << " Select Your Option Please ====> ";
cin >> option;
while (option != 8)
{
switch (option)
{
case 1:
m.build();
break;
case 2:
m.display();
break;
case 3:
m.add();
break;
case 4:
m.deletes();
break;
case 5:
m.edit();
break;
case 6:
m.search();
break;
case 7:
m.sort();
break;
case 8:
exit(0);
cout << "invalid selection" << endl;
}
m.menu();
cout << " Select Your Option Please ====> ";
cin >> option;
}
return;
}
******Employee.h***
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
const int max = 20;
class Employee
{
public:
Employee();
~Employee();
void login();
void menu();
void build();
void display();
void add();
void deletes();
void edit();
void search();
void sort();
private:
char name[20];
long int code;
char designation[20];
int exp;
int age;
int num;
};
#endif
Explanation / Answer
The issue is simple. Yes you are displaying the message that maximum number of entries is 20 but, you are not checking for it. Therefore, you should add a if condition. I am writing new function named "Employee::build()". There is no difference from previous finction except the new if condition. new code would not take more than 20 items. Instead it would print the message " exceed the limit" if user wants to enter more then 20 items. You should replace this function in file.
void Employee::build()
{
cout<< " Build The Table"<<endl;
cout << "maximum number of entries ----- > 20" << endl;
cout << "How many do you want -----> ";
cin >> num;
if(num<=20)
{
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;
}
}
else cout << " Exceed the limit"<<endl<<endl;
cout << " Going to main menu"<<endl<<endl;
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.