[英]How do i match a username with password in c?
我想寫一個登錄程序。 我希望每個用戶名都與其特定的密碼相匹配。 例如,用戶名“Asma”只能使用密碼“Hello123”而不是“Welcome”登錄。
但是不知何故,當我輸入字符串數組中的每個用戶名和密碼時,它會顯示“已成功登錄”。 如何讓每個用戶名都匹配自己的密碼? 另外,當用戶名和密碼都不正確時,如何再次要求有效輸入?
這是我的代碼:
#include <stdio.h>
#include <string.h>
int main(void)
{
char username[10];
char password[10];
printf("Enter your username: ");
scanf("%s", username);
printf("Enter your password: ");
scanf("%s", password);
if(strcmp(username,"Asma") == 0 || strcmp(username,"Iffat") == 0 || strcmp(username,"Kamal") == 0 ){
if(strcmp(password,"Hello123") == 0 || strcmp(password,"Welcome") == 0 || strcmp(password,"fr1324") == 0 ){
printf("Logged In Successfully");
}else{
printf("Incorrect username or password, please try again! ");
}
}else{
printf("Incorrect username or password, please try again! ");
}
如果你想匹配用戶名和密碼,你應該使用&&
來確保兩個檢查都成功:
#include <stdio.h>
#include <string.h>
int main(void)
{
char username[10];
char password[10];
printf("Enter your username: ");
scanf("%s", username);
printf("Enter your password: ");
scanf("%s", password);
if (strcmp(username, "Asma") == 0 && strcmp(password, "Hello123") == 0
|| strcmp(username, "Iffat") == 0 && strcmp(password, "Welcome") == 0
|| strcmp(username, "Kamal") == 0 && strcmp(password, "fr1324") == 0) {
printf("Logged In Successfully");
} else {
printf("Incorrect username or password, please try again! ");
}
}
如何讓每個用戶名都匹配自己的密碼?
您的代碼當前編寫的方式,您所做的就是確保它們匹配任何用戶名和任何密碼 - 您沒有將特定密碼與特定用戶名匹配。
雖然作為第一次嘗試明確檢查每個名稱和密碼是可以的,但它不會擴展——超過 5 個左右的用戶名很快就會變得難以管理。 使用struct
類型創建名稱和密碼的查找表可能會更好:
struct usertable {
char *name;
char *pass;
};
...
struct usertable users[] = { // array size is taken from the number of initializers
{"Asma", "Hello123"},
{"Iffat", "Welcome"},
{"Kamal", "fr1234"},
... // any other entries
{NULL, NULL} // explicitly mark the last entry in the table
};
然后在您輸入用戶名和密碼后,您可以像這樣循環瀏覽表格:
/**
* Cycle through all the entries in users until logged_in is true
* or we hit the end of the table.
*/
int logged_in = 0;
for ( size_t i = 0; users[i].name != NULL && !logged_in; i++ )
{
/**
* Perform a logical AND against the results of strcmp for both
* the username and password, assign the result (0 or 1) to logged_in.
*/
logged_in = (strcmp( username, users[i].name ) == 0 &&
strcmp( password, users[i].pass ) == 0 );
}
if ( !logged_in )
printf( "Username or password not found, try again\n" );
另外,當用戶名和密碼都不正確時,如何再次要求有效輸入?
將以上所有內容放入一個循環中,該循環僅在您匹配密碼或在輸入時發出EOF
信號時才退出(使用 Ctrl-D 或 Ctrl-Z,具體取決於您的系統):
int logged_in = 0;
/**
* Loop until the logged_in flag is true or we see a break statement
*/
while ( !logged_in )
{
printf( "Enter username: " ); // scanf is *not* the right tool for
if ( scanf( "%s", username ) == EOF ) // this, but we'll keep it in place
break; // exit while loop // because otherwise this example
// would be three times as big
printf( "Enter password: " ); // and I don't to bury the main point
if ( scanf( "%s", password ) == EOF ) // of the exercise.
break; // exit while loop
for( size_t i = 0; users[i].name != NULL && !logged_in; i++ )
{
logged_in = (strcmp( users[i].name, username ) == 0 &&
strcmp( users[i].pass, password ) == 0 );
}
if ( !logged_in )
{
fprintf( stderr, "Username or password not found, try again.\n" );
fprintf( stderr, "Type Ctrl-Z to quit.\n" );
}
}
/**
* If logged_in is still 0 at this point, that means the user entered
* Ctrl-Z to quit, and we exit the program.
*/
if ( !logged_in )
{
fprintf( stderr, "Exiting...\n" );
exit( EXIT_SUCCESS ); // "Normal" exit
}
printf( "Logged in successfully\n" );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.