Username and Password program

I am writing a program that should accept value of username and password. The system must check if the entered username and password are valid by checking if the entered values are matched to what are defined in the system. Upon logging in, the system can proceed on asking some information to the users. Only 3 attempts will be allowed, meaning if the user already reached 3 tries of entering invalid username and password the system will display "You exceeded the number of times that you need to input the correct username and password. The system now is locked!" Use appropriate data types in declaring your variables.

And here i am now
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
#include<iostream.h>
#include<string.h>
#include<conio.h>

main(){
	clrscr();
	char Username[30]="sammasangcap",Password[30]="abc123",username[30], password[30],age[2],name[60],sex[6],address[100];

	cout<<"Username:";
	cin>>username;
	if(username.length() <4)
	{
	cout<<"\nUsername length must be atleast 4 characters";
	}
	else
	{
	cout<<"\nPassword:";
	cin>>password;
	}
	if(username == Username && password == Password)
	{
	cout<<"\nLogin Successful!";
	cout<<"\nPlease input the following information:
	cout<<"\nName";
	cin>>name;
	cout<<"\nAge:";
	cin>>age;
	cout<<"\nSex:";
	cin>>sex;
	cout<<"\nAddress:";
	cin>>address;
	}



	getch();
	return 0;


} 

You will want to loop on bad input and continue on good input. Have a read and see if this helps:
http://www.LB-Stuff.com/user-input
@LB

the problem is my compiler won't open <string>
I'm aware of your compiler situation, just use <string.h> - the page I linked you to uses current C++ and not ancient C++ ;)
closed account (48T7M4Gy)
Equality == isn't determined that way with char[] strings. You have to use strcmp() function
You can lookup the reference on this website to see how it works.

if( strcmp(username, Username) == 0 && etc etc
Just edited the program roughly.. Please go through this.

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
#include<iostream.h>
#include<string.h>
#include<conio.h>

int main(){

	char Username[30]="sammasangcap",Password[30]="abc123",username[30], password[30],age[2],name[60],sex[6],address[100];

    unsigned int attempts = 0;
    do
    {
        cout<<"\nUsername:";
        cin>>username;

        if(strlen(username) <4)
        {
            cout<<"\nUsername length must be atleast 4 characters";
        }
        else
        {
            cout<<"\nPassword:";
            cin>>password;
        }
       

        if(!( (strcmp(username,Username) == 0) && (strcmp(password,Password) == 0)))
        {
            cout<<"\nWrong username or password";
            attempts++;
            if(3 == attempts)
            {
                cout<<"You exceeded the number of times that you need to input the correct username and password. The system now is locked!";
                return 0;
            }

        }
        else
        {
            cout<<"\nLogin Successful!";
            cout<<"\nPlease input the following information:";
            cout<<"\nName";
            cin>>name;
            cout<<"\nAge:";
            cin>>age;
            cout<<"\nSex:";
            cin>>sex;
            cout<<"\nAddress:";
            cin>>address;
            break;
        }
    }while(1);

	getch();
	return 0;


}
Last edited on
closed account (48T7M4Gy)
Looks pretty good. It runs OK. I logged on successfully and then tried using wrong information. Worked properly both times.

My system is locked - you have to buy me a new computer now. Just joking.

Good luck at your studies.

PS if(3 == attempts) vs if(attempts == 3)
Last edited on
@kemort thank you very much dude, this is such a big help.
@HabibAurangabad

Thank you bro! god bless you
Why does the compiler says If statement missing @ line 24.
1 more thing, when i use 3 wrong information it won't say that the system is locked.
Thank you everyone for helping me! Much appreciated!!!
closed account (48T7M4Gy)
This is your program which I ran to make a test. Please note I had to change a few things to suit the compiler here online but they are not errors if the programs run OK on your system:

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
#include<iostream>//.h>
#include<string.h>
//#include<conio.h>

using namespace std; ///

int main(){

	char Username[30]="sammasangcap",Password[30]="abc123",username[30], password[30],age[2],name[60],sex[6],address[100];

    unsigned int attempts = 0;
    do
    {
        cout<<"\nUsername:";
        cin>>username;

        if(strlen(username) <4)
        {
            cout<<"\nUsername length must be atleast 4 characters";
        }
        else
        {
            cout<<"\nPassword:";
            cin>>password;
        }
       

        if(!( (strcmp(username,Username) == 0) && (strcmp(password,Password) == 0)))
        {
            cout<<"\nWrong username or password";
            attempts++;
            if(3 == attempts)
            {
                cout<<"You exceeded the number of times that you need to input the correct username and password. The system now is locked!";
                return 0;
            }

        }
        else
        {
            cout<<"\nLogin Successful!";
            cout<<"\nPlease input the following information:";
            cout<<"\nName";
            cin>>name;
            cout<<"\nAge:";
            cin>>age;
            cout<<"\nSex:";
            cin>>sex;
            cout<<"\nAddress:";
            cin>>address;
            break;
        }
    }while(1);

	//getch();
	return 0;


}


This is the output I get when I wanted to deliberately make it fail:

Username:test

Password:fail

Wrong username or password
Username:test

Password:failagain

Wrong username or password
Username:test3

Password:fail3

Wrong username or passwordYou exceeded the number of times that you need to input the correct username and password. The system now is locked! 
Last edited on
Topic archived. No new replies allowed.