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; 
 } 
相关推荐
饮水桶_T38 分钟前
Git笔记
开发语言·笔记·算法
国科安芯38 分钟前
让每一瓦热都听话:航天器主动热控系统CANFD控制网络的可靠性设计
开发语言·网络·单片机·嵌入式硬件·信息与通信·抗辐射·载荷
CoderYanger1 小时前
Java EE 进阶:12.Spring AOP(第一弹)
java·开发语言·spring boot·后端·spring·java-ee·交互
松仔log1 小时前
Kotlin中级——KMP
android·开发语言·kotlin
H H.1 小时前
Java笔记:String字符串
java·开发语言·经验分享·笔记·其他
鲸渔2 小时前
【AI Agent系列课程26-Python 基础26】复盘:Python 接口开发知识清单
开发语言·python·ai编程
sharemylove2 小时前
Java 异常处理 学习笔记
java·开发语言·经验分享·笔记·学习
西西弗Sisyphus2 小时前
Qt QML 程序是怎么跑起来的(1)qml-only
开发语言·c++·qt
Navigator_Z3 小时前
LeetCode //C - 1320. Minimum Distance to Type a Word Using Two Fingers
c语言·算法·leetcode
发疯的技术鼠3 小时前
第21篇-联合和枚举
c语言