Login function

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?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

#include<iostream>
using namespace 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++;
else if(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++;
else if(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;   

}	
Last edited on
Please edit your post to add code formatting tags
[code]
    // { your code here }
[/code]


I don't see a login function in your code. It looks like you already know how to make functions and call them, so show us an attempt of writing it.

Also, prefer const or constexpr ints instead of #defines for constants.

e.g.
const int PERSONS_LIMIT = 50;

And instead of char arrays, you can use std::strings,
1
2
3
4
5
6
7
8
9
class Persons{
public:
  int Id;
  string Name;
  int Age;
  string Dob;
  string Position;
  char Gender;
};

which are a LOT safer to use with cin >>.
Last edited on
Thank you for that, it is my first time here!
Btw please look at my attempt now it shows me errors that "_getch" and "sleep" identifier are not found.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include<iostream>
using namespace std;

void Login() {
    int login();
    while (true) {
        system("cls");
    }
        int login();
        string pass = " ";
        char ch;
        cout << "\n\n\n\n\t\t\t\t\t STAFF MANAGEMENT SYSTEM";
        cout << "\n\n\n\n\n\t\t\t\t\tEnter Your Password :";
        ch = _getch();
        while (ch != 13) {//character 13 is enter
            pass.push_back(ch);
            cout << '*';
            ch = _getch();
        }
        if (pass == "pass") {
            cout << "\n\n\n\t\t\t\t\tLOADING \n\t\t\t\t\t";
            for (int a = 1; a < 8; a++) // Change 'a<?' to how many * you want
            {
                Sleep(500);
                cout << "...";
            }
            cout << "\n\n\n\t\t\t\t\tAccess Granted!! \n\n\n";

            system("PAUSE");
            system("CLS");
        }
        else {
            cout << "\nAccess Aborted...\n";
            int login();
        }
    }
    int login();

    
    const int PERSONS_LIMIT = 50;

    int countPersons = 0;

    class Persons {
    public:
        int Id;
        string Name;
        int Age;
        string Dob;
        string Position;
        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++;
            else if (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++;
            else if (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;

    }

You don't actually need functions like Sleep() and _getch(). They are just fancier ways of manipulating the processing/input/output.

Instead of getch, you can just use something like:
1
2
char ch;
cin >> ch;


However, if you just want the easy way out, add
1
2
#include <windows.h>
#include <conio.h> 

to your program.

int login();This is a function declaration. It doesn't do anything in the case of this program.

1
2
3
    while (true) {
        system("cls");
    }
This is an infinite loop. Your program will not be able to progress past this point.

Somewhere, presumably within your main before your choice loop, you need to call Login().
Last edited on
Thank you very much!!
I want to insert the Login function to appear when the program compiles

I assume that's not what you really mean, right? What purpose would it serve to have to login when compiling your program?
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.

Topic archived. No new replies allowed.