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; 
 } 
相关推荐
csdn_aspnet34 分钟前
C# 求n边凸多边形的对角线数量(Find number of diagonals in n sided convex polygon)
开发语言·算法·c#
qq_254674411 小时前
Docker 中的 镜像(
开发语言
码云社区1 小时前
JAVA二手车交易二手车市场系统源码支持微信小程序+微信公众号+H5+APP
java·开发语言·微信小程序·二手交易·闲置回收
crescent_悦1 小时前
C++:The Largest Generation
java·开发语言·c++
我叫洋洋1 小时前
[Proteus 和 stm32f103c8t6]的使用控制OLED篇]
c语言·stm32·单片机·嵌入式硬件·蓝桥杯·proteus
Book思议-2 小时前
【数据结构】栈与队列全方位对比 + C 语言完整实现
c语言·数据结构·算法··队列
Lyyaoo.3 小时前
【JAVA基础面经】抽象类/方法与接口
java·开发语言
0xDevNull3 小时前
Java实现Redis延迟队列:从原理到高可用架构
java·开发语言·后端
糖炒栗子03263 小时前
Go 语言环境搭建与版本管理指南 (2026)
开发语言·后端·golang
于先生吖3 小时前
无人共享健身房 Java 后端源码 + 多端对接完整方案
java·开发语言