C language password program

Time:2021-10-16

This paper shares the specific code of C language password program for your reference. The specific contents are as follows

Title Requirements

Write code to simulate the user login scenario, and can only log in three times. (you are only allowed to enter the password three times. If the password is correct, you will be prompted to log in. If you enter the wrong password three times, you will exit the program.

logic analysis

3 cycles. If the input is correct, the input is correct and the login is successful. Otherwise, if the input is wrong, please re-enter and cycle three times. If the loop jumps out more than three times, the login fails and the program exits.

Core code

for(i=0;i<3;i++)
 {
 scanf("%s",hsl);
 if(strcmp(hsl,"123456")==0)
 {
 Printf ("login succeeded. \ n");
 break;
 }
 else
 {
 Printf ("password input error, please re-enter: \ n");
 }
 }
 if(3==i)
 {
 Printf ("login failed, program quit. \ n");
 }

All codes

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
 char hsl[10]={0};
 int i=0;
 Printf ("please enter password: \ n");
 for(i=0;i<3;i++)
 {
 scanf("%s",hsl);
 if(strcmp(hsl,"123456")==0)
 {
 Printf ("login succeeded. \ n");
 break;
 }
 else
 {
 Printf ("password input error, please re-enter: \ n");
 }
 }
 if(3==i)
 {
 Printf ("login failed, program quit. \ n");
 }
 system("pause");
 return 0;
}

Show the running results:

A piece of code collected before Xiaobian: C language realizes the password input program. Thank the original author for sharing

Set a user login password in advance, and then let the user enter the password to log in. If it is wrong for three times, the program will exit

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main() {
 int i,x;
 int y = 0; // This variable is used to determine whether the execution of the for loop is correct or wrong
 int code = 147258 ; // Set initial password
 for (i = 0; i < 3; i++) {
 Printf ("please enter password");
 scanf("%d", &x);
 If (x = = code) {// the password entered by the user is consistent with the initial password
 Printf ("login succeeded \ n");
 y = 1; // If the password is correct, the if condition outside the loop will not be executed
 break;
 }
 else {
 Printf ("wrong password, please re-enter \ n");
 }
 }
 if (y = 0) {
 Printf ("error three times, program exit!");
 }
 
 system("pause");
 return 0;
}

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.