今天给大家分享的是前几天写的一个程序,功能是输入用户名和密码,输入的密码用字符’ * '代替,并且用户名与密码无固定长度,并且判断用户名与密码是否正确。

#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<string.h> char user[20]; char USER[20]="123456"; char passed[20]; char PASSED[20]="147258369"; void fun() {	int i=0,j=0; 	char c; loop:	 	printf("User:"); 	scanf("%s",&user); 	if(strcmp(user,USER)==0 ) 	{ 		printf("Passed:"); 		while(1) 		{	 			c=getch(); 			if(c!='\r'&& c!='\b') 			{ 				printf("*"); 				passed[i++]=c; 			} 			else if(c=='\b' && i!=0) 			{ 				printf("\b \b"); 				i--; 				if(i==0) 				{ 					printf("密码不能为空"); 					getch(); 					system("cls"); 				} 			} 			else if(c=='\r') 			{ 				passed[i]='\0'; 				break; 			}	 		} 		if(strcmp(passed,PASSED)==0 ) 		{ 			printf("\n登陆成功\n"); 		} 		else 		{ 			 			system("cls"); 			printf("密码错误,请重新输入!\n"); 			i=0; 			goto loop; 		} 	} 	else 	{	system("cls"); 		printf("用户名不存在,请重新输入!\n"); 		goto loop;  	} 	 } void main() { 	fun();	 } 

希望对大家有所帮助,喜欢的朋友不要忘了点赞哟!!!