C语言——编写代码实现,模拟用户登录情景,并且只能登录三次。

只允许输入三次密码,如果密码正确则提示登录成,如果三次均输入错误,则退出程序。

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <string.h>							//strcmp用到的库函数
int main()
{
	int i = 0;
	char password[20] = {0};
	for(i=0; i<=3; i++)
	{
		printf("输入密码:");
		scanf("%s",password);				//输入的数字是字符串
		if(strcmp(password,"123456") == 0)	//strcmp函数串比较函数,比较是否相等
		{
			printf("登陆成功\n");
			break;
		}
		else
		{
			printf("密码错误\n");
		}
	}
	if(i == 3)
		printf("三次均输入错误,退出程序\n");
    return 0;
}

函数 strcmp 的用法:

cpp 复制代码
函数名: strcmp 
功  能: 串比较 
用  法: int strcmp(char *str1, char *str2); 
程序例: 

#include <string.h> 
#include <stdio.h> 

int main(void) 
 { 
    char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc"; 
    int ptr; 

    ptr = strcmp(buf2, buf1); 
    if (ptr > 0) 
       printf("buffer 2 is greater than buffer 1\n"); 
    else 
       printf("buffer 2 is less than buffer 1\n"); 

    ptr = strcmp(buf2, buf3); 
    if (ptr > 0) 
       printf("buffer 2 is greater than buffer 3\n"); 
    else 
       printf("buffer 2 is less than buffer 3\n"); 

    return 0; 
 } 
相关推荐
Data_Journal1 小时前
Scrapyd:分步教程
开发语言·python·microsoft·golang·编辑器·html·iphone
离陌在学C#2 小时前
C# 异步编程:从 async/await 到 Task 实战指南
开发语言·数据库·c#
羚尔2 小时前
C语言数组
c语言·开发语言
女神下凡2 小时前
芯参谋(11): 各种存储芯片电路设计
开发语言·嵌入式硬件
冬夜戏雪2 小时前
笔试的Scanner in Scanner out
java·开发语言
Zane1994122 小时前
HashSet、TreeSet、LinkedHashSet:底层都是“套壳“
java·开发语言
麻花20133 小时前
Win10、win11清理C盘的bat文件内容
开发语言
露天赏雪3 小时前
掘金小册样章
java·开发语言·人工智能·ai编程
一晌小贪欢4 小时前
python-第15天:Python 列表推导式
开发语言·python·excel·数据可视化·python办公
Java后端的Ai之路4 小时前
17、Python - 观察者模式
开发语言·人工智能·python·观察者模式·外观模式