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; 
 } 
相关推荐
Pniubi1 小时前
Java为什么要分堆和栈
java·开发语言
小小晓.1 小时前
Qt 对话框、QLineEdit 与布局管理实战详解
开发语言·qt
炸薯条!2 小时前
博客系统测试报告
开发语言·lua
Evand J2 小时前
【MATLAB例程12】永磁同步电机基于增广无迹卡尔曼滤波的未知负载转矩在线观测
开发语言·matlab
时间的拾荒人3 小时前
CMake 实战指南:从基础语法到静态库/动态库制作与链接
开发语言·c++·面试
智者知已应修善业3 小时前
【51单片机实现从下到上,从上到下,从中间到两边,两边到中间】2024-10-23
c语言·经验分享·笔记·单片机·嵌入式硬件·51单片机
xyouyuweizhu3 小时前
Java InetAddress 详解:从 IP 地址表示到主机名解析
java·开发语言·python
布莱克60512 小时前
C/C++ 中变量的声明与定义:区别、联系与常见误区
c语言·开发语言·c++·声明·定义
LabVIEW开发12 小时前
LabVIEW 局部变量为何引发竞争条件?四种同步机制
开发语言·labview
benchmark_cc13 小时前
A 股、港股、美股如何建立统一的标的主数据格式?
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash