注册 登录
编程论坛 C++教室

还是密码的问题

amdam23000 发布于 2007-05-13 15:24, 802 次点击

#include<iostream.h>
#include<conio.h>

void main()
{


char a[23];


cout<<endl;
cout<<"请输入您的密码(6——10位数字或字母):";
int index=0;
while(1)
{
char ch;
ch=getch();

if(ch==8) //退格键
{

if(index!=0)
{
cout<<char(8)<<" "<<char(8);
index--;
}
}
else
{if(ch=='\r') //回车键

{a[index]='\0';
cout<<endl;
break;
}

else
{
cout<<"*";
a[index++]=ch;
}
}
}
}




运行时要enter后才能看见之前输入的密码“*******”;
而且不打印出"请输入您的密码(6——10位数字或字母):"这一行 ;当用stdio.h的printf()时才能打印出来,可密码的问题依旧,
都不知怎么回事了~~~~~~求助~~~~

15 回复
#2
amdam230002007-05-13 15:33

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char password[100];
int index=0;
while(1)
{
char ch;
ch=getch();
if(ch==8) //退格键
{
if(index!=0)
{
cout<<char(8)<<" "<<char(8);
index--;
}
}
else if(ch=='\r') //回车键
{
password[index]='\0';
cout<<endl;
break;
}
else
{
cout<<"*";
password[index++]=ch;
}
}
cout<<"password:"<<password<<endl;
return 0;
}

上面是一位高手写的,我做了这样的修改:

#include<iostream.h>
#include<conio.h>
void main()
{
char password[100];
int index=0;
while(1)
{
char ch;
ch=getch();
if(ch==8) //退格键
{
if(index!=0)
{
cout<<char(8)<<" "<<char(8);
index--;
}
}
else if(ch=='\r') //回车键
{
password[index]='\0';
cout<<endl;
break;
}
else
{
cout<<"*";
password[index++]=ch;
}
}
cout<<"password:"<<password<<endl;
}

然后就出现上面所说的情况了,这又是为什么呢?????


#3
踏魔狼2007-05-13 15:51
在每次cout后调用flush.
例如:
cout<<"print"<<endl;
cout.flush();
#4
amdam230002007-05-13 15:52

这只是要简单地实现密码的以屏蔽的方式输入,然后又要能够用backspace和enter键来修改和确定这些密码~~~~~~有些程序要用到这个功能~~~

#5
amdam230002007-05-13 16:03
我用了cout.flush();  ,但还是不行啊~~~~~这和缓冲没有关系吧?
#6
踏魔狼2007-05-13 16:11
char pw[100];
cout<<"请输入您的密码(6——10位数字或字母):";
cout.flush();
for(int i=0;i<100;i++){
pw[i]=getch();
cout<<"*";
cout.flush();
if(pw[i]=='\r')
break;
}
pw[i]='\0';
cout<<"password:"<<pw<<endl;
cout.flush();

[此贴子已经被作者于2007-5-13 16:12:17编辑过]

#7
amdam230002007-05-13 16:24
谢谢了~~~~但是我想要的主要是实现 Backspace 的原本功能,这是最关键的~~~你写的可以实现enter的确定并停止密码输入,可我要的是可以修改~~~
#8
yuyunliuhen2007-05-13 17:12

还不行么?

[此贴子已经被作者于2007-5-13 17:14:52编辑过]

#9
amdam230002007-05-13 17:22
是啊~~~
#10
踏魔狼2007-05-13 17:25

主要是按BackSpace能清除一个*.
不太清楚如何对以输出的数据进行清除.

#11
yuyunliuhen2007-05-13 17:25
以下是引用amdam23000在2007-5-13 15:24:14的发言:

#include<iostream.h>
#include<conio.h>

void main()
{


char a[23];


cout<<endl;
cout<<"请输入您的密码(6——10位数字或字母):";
int index=0;
while(1)
{
char ch;
ch=getch();

if(ch==8) //退格键
{

if(index!=0)
{
cout<<char(8)<<" "<<char(8);
index--;
}
}
else
{if(ch=='\r') //回车键

{a[index]='\0';
cout<<endl;
break;
}

else
{
cout<<"*";
a[index++]=ch;
}
}
}
}




运行时要enter后才能看见之前输入的密码“*******”;
而且不打印出"请输入您的密码(6——10位数字或字母):"这一行 ;当用stdio.h的printf()时才能打印出来,可密码的问题依旧,
都不知怎么回事了~~~~~~求助~~~~

#include<iostream>
using namespace std; //
#include<conio.h>
void main()
{
char a[23];


cout<<endl;
cout<<"请输入您的密码(6——10位数字或字母):";
int index=0;
while(1)
{
char ch;
ch=getch();

if(ch==8) //退格键
{

if(index!=0)
{
cout<<char(8)<<" "<<char(8);
index--;
}
}
else
{if(ch=='\r') //回车键

{a[index]='\0';
cout<<endl;
break;
}

else
{
cout<<"*";
a[index++]=ch;
}
}
}
}

应该可以实现了吧,下面是图片:

只有本站会员才能查看附件,请 登录

#12
amdam230002007-05-13 17:32

你的是:

#include<iostream>
using namespace std; //
#include<conio.h>

这样是可以,但我用的是

#include<iostream.h>
#include<conio.h>

这样的话在主函数内怎样才能实现如上的功能?

#13
amdam230002007-05-13 17:36

不声明命名空间时怎么办?

#14
yuyunliuhen2007-05-13 17:53
#include<iostream.h>
#include<conio.h>
iostream.h,conio.h;名字空间冲突吧,这个我也不大清楚了,为什么会出现这样的结果。

一般的老的版本的编译器还支持#include<iostream.h>这个形式了,象VC++2005,GCC这样写就会报错
最新标准
#include<iostream>
{
std:cout<<"";
//code here
}
为了不那么多麻烦,大家都愿意这样写
#include<iostream>
using namespace std;

{
//code here
}
#15
amdam230002007-05-13 21:07

那好了,谢谢

1