C++用*显示输入密码的代码怎么写 (用C++,C的不要 ),急用,谢谢
作者:未知侠名时间:2022-11-27 04:59:09
楼上错了.楼主要的效果完全可以实现.
可以用c = getch();
然后用cout<< "\b"将之前输入的字符删去,再cout<<"*"
http://www.cplusplus.com/forum/general/4237/ 这个是我用c++写的 #include#include"fstream" using namespace std; class message { public: char msg[200]; friend ostream & operator<<(ostream &out,message &s); friend istream & operator>>(istream &in,message &s); }; ostream & operator<<(ostream &out,message &s) { out<<<'\n'; return out; } istream & operator>>(istream &in,message &s) { in>>s.msg; return in; } int main() { ofstream ofile; ifstream ifile; message s; ifile.open("message.txt"); if(ifile.fail()) { ofile.open("message.txt"); cout<<"这是一个留言程序,请留言:"<>s; ofile<>s; while(!ifile.eof()) { cout<>s; } ifile.close(); for(;;) { int option; cout<<"please choose:"<<<"1-------------------add message"<<<"2-------------------cover ever message"<<<"3-------------------print message"<<<"0-------------------exit"<>option; switch(option) { case 1: ofile.open("message.txt",ios::app); cout<<"please input a message:"<>s; ofile<>s; ofile<>s; while(!ifile.eof()) { cout<>s; } ifile.close(); break; case 0: ofile.close(); return 0; } } } return 0; } 呵呵,楼下的完全正确!非常不好意思!!我愧对兄弟们!!!!
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
void main()
{
int c;
string password = "";
do //Loop until 'Enter' is pressed
{
c = getch();
switch(c)
{
case 0:
{
break;
}
case '\b':
{
if(password.size() != 0) //If the password string contains data, erase last character
{
cout << "\b \b";
password.erase(password.size() - 1, 1);
}
break;
}
default:
{
if(isalnum(c) || ispunct(c))
{
password += c;
cout << "*";
if(password.compare("hehe")==0){
cout<<"\n成功"<<endl;
return;
}
}
break;
}
};
}
while(c != '\r');
}
可以用c = getch();
然后用cout<< "\b"将之前输入的字符删去,再cout<<"*"
http://www.cplusplus.com/forum/general/4237/ 这个是我用c++写的 #include#include"fstream" using namespace std; class message { public: char msg[200]; friend ostream & operator<<(ostream &out,message &s); friend istream & operator>>(istream &in,message &s); }; ostream & operator<<(ostream &out,message &s) { out<<<'\n'; return out; } istream & operator>>(istream &in,message &s) { in>>s.msg; return in; } int main() { ofstream ofile; ifstream ifile; message s; ifile.open("message.txt"); if(ifile.fail()) { ofile.open("message.txt"); cout<<"这是一个留言程序,请留言:"<>s; ofile<>s; while(!ifile.eof()) { cout<>s; } ifile.close(); for(;;) { int option; cout<<"please choose:"<<<"1-------------------add message"<<<"2-------------------cover ever message"<<<"3-------------------print message"<<<"0-------------------exit"<>option; switch(option) { case 1: ofile.open("message.txt",ios::app); cout<<"please input a message:"<>s; ofile<>s; ofile<>s; while(!ifile.eof()) { cout<>s; } ifile.close(); break; case 0: ofile.close(); return 0; } } } return 0; } 呵呵,楼下的完全正确!非常不好意思!!我愧对兄弟们!!!!
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
void main()
{
int c;
string password = "";
do //Loop until 'Enter' is pressed
{
c = getch();
switch(c)
{
case 0:
{
break;
}
case '\b':
{
if(password.size() != 0) //If the password string contains data, erase last character
{
cout << "\b \b";
password.erase(password.size() - 1, 1);
}
break;
}
default:
{
if(isalnum(c) || ispunct(c))
{
password += c;
cout << "*";
if(password.compare("hehe")==0){
cout<<"\n成功"<<endl;
return;
}
}
break;
}
};
}
while(c != '\r');
}
- 站长提醒大家:以上内容,未识真伪,仅供参考。理性上网,以免过度。谨慎广告,危及钱财。
版权声明:本信息由系统自动搜集网络信息,本站尊重网络信息的版权问题,本站所有内容仅供学习使用,版权归原作者所有,若有来源错误或者侵犯您的合法权益,您可通过邮箱与我们取得联系,我们将及时进行处理。