Login system

Dec 27, 2014 at 4:10am
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 Dec 27, 2014 at 4:10am
Dec 27, 2014 at 4:46am
I believe that the != should be ==
Dec 27, 2014 at 5:15am
I believe the != should be strcmp.
Dec 27, 2014 at 5:17am
ok have you checked other posts
Dec 27, 2014 at 5:24am
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
Dec 27, 2014 at 7:30am
I thought != is not equal?
Dec 27, 2014 at 7:40am
Hello? C is calling me, he's asking for strcmp. Does anybody know strcmp here?
Dec 27, 2014 at 11:34am
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 Dec 27, 2014 at 11:34am
Dec 27, 2014 at 12:36pm
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.
Dec 27, 2014 at 1:29pm
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)
Dec 28, 2014 at 7:31am
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.
Dec 28, 2014 at 12:14pm
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?
Dec 28, 2014 at 1:59pm
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
Dec 28, 2014 at 3:15pm
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 Dec 28, 2014 at 3:23pm
Dec 28, 2014 at 5:38pm
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.