Login system

How do you make a login system on stdio.h. I am using c by the way.

1
2
3
4
5
6
7
8
9
10
11
char username[100];
char password[10];
printf("Enter Username: ");
scanf("%99s", &username);
if (username != "jane1234")
{
printf("Invalid Username. Please try again.\n");
}
else
{
printf("Enter Password: ");

Whenever I run the program and type in jane1234 as the username, it keep saying "Invalid Username. Please try again.". Anything wrong with the code? Any help is appreciated. Thanks.
Last edited on
I believe that the != should be ==
I believe the != should be strcmp.
ok have you checked other posts
oops here's your problem
1
2
3
4
if (username != "jane1234")
{
printf("Invalid Username. Please try again.\n");
}

your code says if the username is the same as jane1234 than the username is invalid when it is actually correct
I thought != is not equal?
Hello? C is calling me, he's asking for strcmp. Does anybody know strcmp here?
sorry, this is in c++ but hope you can understand it ! it worked for me .
cout is printf.
cin is scanf.
==:equals.
!= different.
&& and.

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

#include <iostream>

int main()
{
    using std::cout;
    using std::cin;
    using std::string;

    string username = "";
    string password = "";
    cout << "enter your username \n";
    cin >> username;

    cout << "enter your password \n";
    cin >> password;

if (username == "jane1234" && password == "anything")
{
    cout << "correct username and password \n";
}
else if (username == "jane1234" && password != "anything")
{
    cout << "incorrect password \n";
}
else if (username != "jane1234" && password == "anything")
{
    cout << "incorrect username\n";
}
else
{
    cout << "inorrect username or password \n";
}
return 0;
}

Last edited on
Can the code be made so that if the user type in the wrong username, it will say the username is incorrect instead of asking for password. If the username is typed correctly, it will prompt the user to enter their password.
FFS... Good work ignoring people who hinted to an actual solution, plus confusing answers.
The guy doesn't use C++, std::string::operator== is useless.
Also you do not compare strings with == or with !=, but you use STRCMP.
1
2
3
4
5
6
7
8
if(strcmp(name, "yourusername") == 0)
{
    printf("correct!");
}
else
{
    printf("bad!");
}

C++'s std::string allows you to use ==.
Otherwise you're just comparing data location, not their content (eg, is data stored in variable "name" at the same memory position of constant "yourusername"? probably not, but what matters is the content of the locations, not the location itself)
Sorry if I ignored you, I didn't mean to ignore you. Is it possible to just include stdio.h only without string.h to come out with the code? Just a question. Thanks.
The only other way is to build a clone of strcmp.
Another alternative is memcmp if you know the length of the string... But still requires string.h.
Why do you not want to use string.h?
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
#include <stdio.h>

int equals( const char* a, const char* b )
{
    if( a == NULL || b == NULL ) return a == NULL && b == NULL ; // equal if both are NULL

    // invariant a and b are both non-NULL
    for( ; *a && *b ; ++a, ++b ) if( *a != *b ) return 0 ; // not equal if there is a mismatch

    // invariant: all characters up to the end of either one of the two strings are equal
    return *a == 0 && *b == 0 ; // equal if we have reached the end of both strings
}

int main()
{
    const char expected_username[] = "jane1234" ;

    enum { MAX_USERNAME_SZ = 99 } ; // avoid magic numbers
    char username[ MAX_USERNAME_SZ + 1 ] = "" ; // +1 for the terminating null character

    char format[32] = "" ; // avoid magic numbers in the format string too
    sprintf( format, "%%%us", MAX_USERNAME_SZ ) ; // writing maintainable code in C is hard 
                                                  // especially when i/o is involved

    int fields_read = scanf( format, username ) ; // **** not &user_name ****

    if( fields_read == 1 ) 
    {
        printf( "%s: ", username ) ;
        if( equals( username, expected_username ) ) puts( "correct" ) ;
        else puts( "incorrect" ) ;
    }
}

http://coliru.stacked-crooked.com/a/eab5fc91e12f73cd
I'd done this using simplest functions I'd learn in my starting days with C.

1
2
3
4
5
6
7
8
9
10
11
12
names={'Tani','Prince'};
passes=[123,345];
printf("Enter name: ");
scanf("%s",&a);
if(name=='Tani')
x=0;
else if(name=='Prince')
x=1;
else
printf("wrong user entry.");
if(name==names[x]&&pass==passes[x])
xxxxxxxxxxxxxxxxxxxxxxxx

this way I'd created atm login system, you can get more ideas via it :) Here basically you can create as many usernames & passwords & change them right from where they were declared in the start of the source code :)
Last edited on
Except that it doesn't look like C at all, especially the first two lines.
You can safely follow JLBorges' code.
I'd just change line 5's "then" statement to
return a==b;
And line 11 to
return *a == *b;
Topic archived. No new replies allowed.