Password checking program

Threads: Write a program that checks the user password when logging. Users must log in to when the correct username and password out loud. (username and password defined in the program)

In reality, You will encounter this problem a lot when building software, wesbite that function login. So this problem is extremely helpful.

First you need to realize that if a user enters the correct okay, if a user enters the wrong, they must re-enter, ie must be repeated many times. So how many times to repeat? Repeat until the new right only -> can not know in advance how many times repeat. So we have to use something? Using loop that unforeseen occurrence number -> Use while hoặc do while.

Now you remember the meaning of the while loop and do while:

  • while: While something right, do certain work.
  • do while: Work while something is still true.

Of 2 this structure is quite similar in meaning and usage so you can use which are. But look closely, we will see the difference.

Code using a while loop:

/**
*	Program login, enter username and passwrod until they correct
*/

#include <stdio.h>
#include <string.h>	// for compare string by strcmp

int main() {

	// username and password we have
	char username[] = "nguyenvanquan7826";
	char passwrod[] = "0codauem";

	// username and password user must enter to login
	char user[50], pass[50]; 

	printf("Enter your username: ");
	gets(user);

	printf("Enter your password: ");
	gets(pass);

	while(strcmp(user, username) != 0 || strcmp(pass, passwrod) != 0) {
		printf("\nusername or passwrod incorrect\n");

		printf("Enter your username: ");
		gets(user);

		printf("Enter your password: ");
		gets(pass);
	}

	printf("Login success!\n");

	return 0;
}

In the above code you note some points:
Using the library string.h to use the string comparison function strcmp.
By username and password string should we use gets to accession should not use scanf.
Within the while loop by checking conditions before, so we have to have something for it check, so we need to enter your username and password in the previous. This makes our code was admitted once again at the top before using while loop. But we also can not enter before there, như vậy chuỗi user và pass ban đầu sẽ rỗng.

Sau đây là code mà chúng ta sử dụng do-while.

/**
*	Program login, enter username and passwrod until they correct
*/

#include <stdio.h>
#include <string.h>	// for compare string by strcmp

int main() {

	// username and password we have
	char username[] = "nguyenvanquan7826";
	char passwrod[] = "0codauem";

	// username and password user must enter to login
	char user[50], pass[50]; 

	int count = 0; // Counting the number of login times

	do{

		if(count > 0) printf("\nusername or passwrod incorrect\n");

		printf("Enter your username: ");
		gets(user);

		printf("Enter your password: ");
		gets(pass);

		count++;

	}while(strcmp(user, username) != 0 || strcmp(pass, passwrod) != 0);
	
	printf("Login success!\n");

	return 0;
}

Trong code này, chúng ta sử dụng thêm biến count để đếm số lần login, nếu lần đầu tiên thì không hiện ra là mật khẩu sai, chỉ hiện từ lần thứ 2 trở đi mà thôi.

Basically, bài này khá dễ dàng. Hi vọng các bạn có thể nắm vững 🙂