四有青年想法:写个C程序

来源:百度文库 编辑:中科新闻网 时间:2024/04/23 14:45:18
要能在VC++中运行:::::::::::::::::::::::::
就是要能实现在运行时用户输入的字符显示为*,就像输入密码那样的效果.怎么实现啊?写一个看看.

/*Password*/
#include<stdio.h>
#include<string.h>
main()
{
int a[4],password[4]={8,8,8,8},n,r,s;
clrscr();
for(n=1;n<4;n++)
{if(n==1) printf("Please enter your password:");
else printf("Enter again:");
for(r=0;r<=3;r++)
{a[r]=getch() ;
printf("*") ;
}
s=strcmp(a,password);
if (s==0&&n<4)
{printf("You are welcome!\n"); break; }
else if (n==3) printf("\nYou have entered three times! You are not welcome!\n");
else {printf("\nWrong password!\n"); continue ;}
}
getch();
}

给你举个简单的例子:)
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{char psw[50];
int k=0;
printf("Enter password:\n");
while(1){
psw[k]=getch();
if(psw[k]=='\xD')
{psw[k]='\0';break;}
k++;
printf("*");
}
if(strcmp("admin",psw)==0)
printf("\nRight password!\n");
else printf("\nInvalid password!\n");
getch();
return 0;
}

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

#define DEBUG_ON

int main(int argc,char argv[]){/* 命令行参数*/
char Ptmp[33];
FILE* fp;
int i=0;
char tmp[50];/*文件名缓冲*/

sprintf(tmp,"%s\\%s","c:\\","tmp.p");/*把预定的文件名写入文件名缓冲中*/
fp=fopen(tmp,"w");/*打开保存密码的文件*/

memset(Ptmp,0,33);/*给密码缓冲分配地址*/
if(fp)/*如果打开文件成功*/
for(i=0;i<32;i++){
Ptmp[i]=getch();
putc(Ptmp[i],fp);

if(Ptmp[i]=='\n' ¦¦ Ptmp[i]=='\r'){/*输入密码结束*/
Ptmp[i]='\0';
#ifdef DEBUG_ON
printf("\n%s\n",Ptmp);
#endif
break;
}
else if(i==32-1){/*输入密码大于最大长度*/
printf("password more then max password length!\n");
break;
}
else{/*显示'*'号*/
putc('*',stdout);
}
}
if(fp) {/*关闭文件*/
putc('\n',fp);
fclose(fp);
}
return 0;
}
看看行不?

有现成的啊
控件上点右键-styles-password