Hello everyone, in the code below I tried to make a simple staff management program for school, I want to insert the Login function to appear when the program compiles, and then continue in the other parts. Can someone please tell me how to do that?
#include<iostream>
usingnamespace std;
#define PERSONS_LIMIT 50
int countPersons = 0;
class Persons{
public:
int Id;
char Name[20];
int Age;
char Dob[20];
char Position[30];
char Gender;
};
Persons obj[PERSONS_LIMIT];
void input(){
if(countPersons<PERSONS_LIMIT)
{
cout<<"\n Enter id (number): ";
cin>>obj[countPersons].Id;
cout<<"\n Enter Name (20 characters): ";
cin>>obj[countPersons].Name;
cout<<"\n Enter Age (number): ";
cin>>obj[countPersons].Age;
cout<<"\n Enter DOB (dd-mm-yy): ";
cin>>obj[countPersons].Dob;
cout<<"\n Enter Position : ";
cin>>obj[countPersons].Position;
cout<<"\n Enter Gender (M/F) : ";
cin>>obj[countPersons].Gender;
countPersons++;
}
else
{
cout<<"\n Error : Limit is only " << PERSONS_LIMIT;
}
}
void printAll(){
cout<<"\n **** **** Printing All Records **** ****";
cout<<"\n total number of persons : "<<countPersons<<endl;
for(int i=0;i<countPersons;i++){
cout<<"\n Id : "<< obj[i].Id;
cout<<"\t Name : "<<obj[i].Name;
cout<<"\t Age : "<<obj[i].Age;
cout<<"\t DOB : "<<obj[i].Dob;
cout<<"\t Position: "<<obj[i].Position;
cout<<"\t Gender : "<<obj[i].Gender;
}
}
void printbyAge(){
cout<<"\n **** **** Printing All Records by Age********";
int count50plus =0;
int count40plus=0;
int lessthen40=0;
for(int i=0;i<countPersons;i++){
if(obj[i].Age>50)
count50plus++;
elseif(obj[i].Age>40)
count40plus++;
else
lessthen40++;
}
cout<<"\n Persons more than 50 : "<<count50plus;
cout<<"\n Persons more than 40 : "<<count40plus;
cout<<"\n Persons less than 40 : "<<lessthen40;
}
void printSexCount(){
cout<<"\n **** **** Printing All Records by Sex Count ********";
int malecount{};
int femalcount{};
for(int i=0;i<countPersons;i++){
if(obj[i].Gender =='M')
malecount++;
elseif(obj[i].Gender=='F')
femalcount++;
}
cout<<"\n Number of Male : "<< malecount;
cout<<"\n Number of Female : "<< femalcount;
}
int main(){
int choice = -1;
while(choice!=0){
cout<<"\n\n ============Program Menu==========";
cout<<"\n 1 Input Records ";
cout<<"\n 2 Print All Records";
cout<<"\n 3 Print by Age";
cout<<"\n 4 Print by Sex count";
cout<<"\n 0 to exit";
cout<<"\n Enter you choice : ";
cin>>choice;
switch(choice){
case 1: input(); break;
case 2: printAll();break;
case 3: printbyAge(); break;
case 4: printSexCount(); break;
case 0: cout<<"\n thank you for using software !!";break;
default: cout<<"\n Error: Invalid Selection";
}
}
return 0;
}
A tip for making your code cleaner and better looking:
You can add a function in the Persons class to print all the info of a person, for instance let's say "printInfo()" by copying almost all the code you have inside the printAll() function to the new printInfo() function, then your printAll() function only goes through the for loop by calling the printInfo() function for every person in your array. This is going to make your life easier when you get to a point where you want to print the information of one specific person, so you only call that function and it'll do the job without replicating the code you have already written in another part of your program.
the function would be something like this:
1 2 3 4 5 6
//this function goes inside the persons class
void printInfo(){
cout<< "\n Id : " << id << "\n Name : " << Name << "\t Age : " << Age << "\t DOB : " << Dob << "\t Position: " << Position << "\t Gender : " << Gender;
}
And your printAll() function could be rewritten like this:
1 2 3 4 5 6 7 8 9 10
void printAll() {
cout << "\n **** **** Printing All Records **** ****";
cout << "\n total number of persons : " << countPersons << endl;
for (int i = 0; i < countPersons; i++) {
obj[i].printInfo();
}
}
I haven't tested it (i'm a noob lol) but as far as I know it should work perfectly fine. Then if you later want to print a specific person's information you will only need to call that persons printInfo() function.