topic:
Write code to simulate the user login scenario , and can only log in three times .
Only three passwords are allowed ,
If the password is correct , the login is successful .
If the input is wrong three times , the program exits .
=========================================================================
Ideas:
general idea:
(1). Generate related variables and character arrays for storing input data ,
Then set a variable flag to make it easier to judge whether the passwords are consistent later .
If it is consistent , change the flag to 1 , and if it is inconsistent , it will still be 0 .
(2). Use the for loop to input data three times .
(3). Set the if condition judgment statement in the for loop to judge the data three times in a loop .
(4). After jumping out of the for loop , check the variable flag and print the exit program information .
first step:
(1). Generate relevant variables .
(2). Generate a character array to store the input data .
(3). Set a variable flag ,
It is convenient to judge whether the password is consistent later ,
If it is consistent , change the flag to 1 , and if it is inconsistent , it will still be 0 .
Implementation code:
#include <stdio.h> int main() { //生成相关变量: int i = 0; //循环变量 //生成存放输入数据的字符数组 char password[20] = { 0 }; //设置一个变量flag: int flag = 0; return 0; }
Realize the picture:
Step two:
(1). Use the for loop to input data three times .
(2). The input string can be used: scanf("%s", password);
Because password is the name of the array and the first address of the array , there is no need to add the address character & .
Implementation code:
#include <stdio.h> int main() { //生成相关变量: int i = 0; //循环变量 //生成存放输入数据的字符数组 char password[20] = { 0 }; //设置一个变量flag: int flag = 0; //使用for循环,循环输入三次数据: for (i = 0; i < 3; i++) { //输入前先打印提示信息: printf("请输入密码:"); //输入数据: scanf("%s", password); //因为password是数组,数组名是数组首地址,所以不用取地址符& } return 0; }
Realize the picture:
third step:
(1). Set the if condition judgment statement in the for loop to judge the data three times in a loop .
(2). Determine whether the passwords ( strings ) are equal :
You cannot use == (equal to) to compare two strings , you should use
strcmp(Character array name, "string to compare")
For library functions , a header file needs to be added : <string.h> .
strcmp returns 0 : indicating that the two strings are equal ;
strcmp returns a number >0 : indicating that the first string is greater than the second string ;
strcmp returns a number <0 : indicating that the first string is less than the second string .
String comparison size : the comparison is the ASCII value of the character at the corresponding position .
like:
Comparison of abcd and abq :
First digit : a == a
Second bit : b == b
Third place : c < q
The ASCII value of c is less than the ASCII value of q
So abcd < abq
(3). If the password is correct , print the corresponding information , set the flag to 1 , and use break to break out of the loop .
(4). If the password is wrong , print the corresponding information .
Implementation code:
#include <stdio.h> #include <string.h> int main() { //生成相关变量: int i = 0; //循环变量 //生成存放输入数据的字符数组 char password[20] = { 0 }; //设置一个变量flag: int flag = 0; //使用for循环,循环输入三次数据: for (i = 0; i < 3; i++) { //输入前先打印提示信息: printf("请输入密码:"); //输入数据: scanf("%s", password); //因为password是数组,数组名是数组首地址,所以不用取地址符& //设置if条件判断语句循环判断三次数据 if (strcmp(password, "123456") == 0) //假设密码是 123456 ,如果相等会返回 0,说明输入的和密码相同 { printf("登录成功\n"); flag = 1; break; //成功则跳出循环 } else { printf("密码错误\n"); } } return 0; }
Realize the picture:
the fourth step:
After jumping out of the for loop , check the variable flag and print the exit program information .
Implementation code:
#include <stdio.h> #include <string.h> int main() { //生成相关变量: int i = 0; //循环变量 //生成存放输入数据的字符数组 char password[20] = { 0 }; //设置一个变量flag: int flag = 0; //使用for循环,循环输入三次数据: for (i = 0; i < 3; i++) { //输入前先打印提示信息: printf("请输入密码:"); //输入数据: scanf("%s", password); //因为password是数组,数组名是数组首地址,所以不用取地址符& //设置if条件判断语句循环判断三次数据 if (strcmp(password, "123456") == 0) //假设密码是 123456 ,如果相等会返回 0,说明输入的和密码相同 { printf("登录成功\n"); flag = 1; break; //成功则跳出循环 } else { printf("密码错误\n"); } } //跳出 for循环 后,看变量flag情况打印退出程序信息。 if (flag == 0) { printf("退出程序");//密码错误 则 退出程序 } return 0; }
Realize the picture:
Final code and implementation effect
Final code:
#include <stdio.h> #include <string.h> int main() { //生成相关变量: int i = 0; //循环变量 //生成存放输入数据的字符数组 char password[20] = { 0 }; //设置一个变量flag: int flag = 0; //使用for循环,循环输入三次数据: for (i = 0; i < 3; i++) { //输入前先打印提示信息: printf("请输入密码:"); //输入数据: scanf("%s", password); //因为password是数组,数组名是数组首地址,所以不用取地址符& //设置if条件判断语句循环判断三次数据 if (strcmp(password, "123456") == 0) //假设密码是 123456 ,如果相等会返回 0,说明输入的和密码相同 { printf("登录成功\n"); flag = 1; break; //成功则跳出循环 } else { printf("密码错误\n"); } } //跳出 for循环 后,看变量flag情况打印退出程序信息。 if (flag == 0) { printf("退出程序");//密码错误 则 退出程序 } return 0; }
Realize the effect:
Summarize:
Learn the use of strcmp() library function :
It is used to judge whether two strings are equal , and a header file needs to be added : <string.h> .
strcmp(Character array name, "string to compare")
strcmp returns 0 : indicating that the two strings are equal ;
strcmp returns a number >0 : indicating that the first string is greater than the second string ;
strcmp returns a number <0 : indicating that the first string is less than the second string .
String comparison size : the comparison is the ASCII value of the character at the corresponding position .