当前位置:网站首页>C language case: password setting and login > clear solution getchar and scanf
C language case: password setting and login > clear solution getchar and scanf
2022-07-27 19:18:00 【Liu jingyiru】
(1)getchar It is specially used for input and output of character data . And by default, only one character is processed at a time, but you can also get strings . The characters entered will return ASII value .48-57 Is a digit . Can be between 0~127 A decimal integer between ( contain 0 and 127)( exceed 127 It's not ASCII code )
2. How to use getchar Get string
char src[10];char input=getchar(); Whether it's scanf still getchar still gets, Are the last to \0 a null-terminated string .
\\ The first input
char ch[]="abcde";( The last one behind defaults \0, Because of the definition , Default exists )
\\ The second input
char ch[]={"'a' ,'b','c','d','e'}";
\\ Output
printrf("%d\n",strelen(char));The second input method , Is a character array ,strelen String length cannot be caught \0, So it will be printed randomly , Stop at random value . Will produce many random values that do not meet expectations .
3. Principle introduction :scanf and getchar There are buffers when entering content while waiting , And take the input characters from the buffer to complete the reading .
The difference is that ,scanf You can take more ,getchar Only one can be taken and the access type is limited to character .
4. application
Here are a few situations :
(1) Input complete ,ctrl+z End input ;
(2) When we log in to the system, we often encounter , Enter a character in the keyboard to select , because getchar() The limitation that only one character can be obtained , therefore scanf Left after taking the string \n , Will be getchar Getting causes the next selection operation to fail .
[scanf Function encountered while reading a string tab And spaces stop reading , But you can enter , encounter \n End input and reading . That is why we press enter after entering .] but gets() The function will receive the entire string of input until it encounters Line break until , You can try this .
Our thinking is , Clear the missing characters in the buffer \n. So we need to use a cleanup operation . If the system does not come with , We need to use our brains to write by ourselves . Here we take getchar To pick \n,
The code is as follows :
while((temp=getchar())!='\n') // The explanation is that when the result we pick up again is not empty ,
// Think the buffer has been cleared to empty
{ ;/// Logic is empty
}(3)EOF(End Of File) End of file flag . We tend to be in putchar I will judge first , Whether our status is entered and the file is read successfully .
while((ch=get char()!=EOF))
{ putchar(char);
}(4)int char=get char();
Here we use int The reason is that ,getchar The essential type of the return value is ASII value , If there is a read error, it will also return EOF, namely -1.
(5) Example show :
#include<stdio.h>
#include<stdlib.h>
int main()
{
char input[100] = { 0 };
printf(" Please enter your password :>");\\ Set the password
scanf("%s",input);
int temp = 0;
while ((temp = getchar()) != '\n')
{
;
}
printf(" Please confirm your password , Input from keyboard (Y/N) choice ");\\ Confirm password
int ch = getchar();
if (ch == 'Y')
{
printf(" Confirm success !\n");
}
else { printf(" Confirmation failed !"); }
again: printf(" Please enter your password again to login :>\n");\\ Second login
char put[100] = { 0 };
scanf("%s", put);
if ((strcmp("20020225", put)) == 0)
{
printf(" The password is correct ");
}
else
goto again;
}边栏推荐
- Self control principle learning notes - system stability analysis (2) - loop analysis and Nyquist bode criterion
- Kinect2 for Unity3D——AvatarDemo学习
- Definition of graph traversal and depth first search and breadth first search (2)
- Power control
- During the interface test, connect to the database and back up, restore and verify the data source
- normal distribution, lognormal distribution,正态随机数的生成
- kettle 合并记录 数据减少
- C # one method returns multiple values. Suggestions collection
- PHP字符串操作
- Jmeter接口自动化-如何解决请求头Content-Type冲突问题
猜你喜欢

What if idea successfully connects to the database without displaying the table

ES6 learning notes (1) - quick start

Webmagic+selenium+chromedriver+jdbc垂直抓取数据。

C语言案例:密码设置及登录> 明解getchar与scanf

2022 preparation for autumn recruitment 10W word interview sketch PDF version, with operating system and computer network interview questions

Cyclic multi-Variate Function for Self-Supervised Image Denoising by Disentangling Noise from Image

进行接口测试时,连接数据库,对数据源进行备份、还原、验证操作

图的遍历的定义以及深度优先搜索和广度优先搜索(二)

Express get/post/delete... Request

Using vscode to build u-boot development environment
随机推荐
Normal distribution, lognormal distribution, generation of normal random numbers
ES6学习笔记(1)——快速入门
[Luogu p3175] bitwise OR (min max inclusive) (high-dimensional prefix and / FWT)
电磁场学习笔记-矢量分析和场论基础
自控原理学习笔记-系统稳定性分析(1)-BIBO稳定及Routh判据
Some advice for NS2 beginner.
Webmagic+selenium+chromedriver+jdbc垂直抓取数据。
SSM integration
IDEA连接数据库时区问题,报红Server returns invalid timezone. Need to set ‘serverTimezone‘ property.
Unity-显示Kinect深度数据
How can I get started quickly when I change my career to soft testing and job hopping to a new company?
Unity display Kinect depth data
Latex使用--subfigure竖排图形
js常用utils封装
During the interface test, connect to the database and back up, restore and verify the data source
kettle学习——8.2版本的资源库配置变为灰色,且没有了Connect按钮
Latex use - subfigure vertical graphics
图的遍历的定义以及深度优先搜索和广度优先搜索(二)
How to break the team with automated testing
kettle8.2 安装及常见问题