首先是准备工作(安装C++不用我说)
#include<bits/stdc++.h>
#define _CRT_SECURE_NO_WARNINGS
#include<windows.h>
using namespace std;
如果你需要做一个可注册新用户的系统,则一定要用文件存储它,我这里使用Username.in文件
**注意:这里一定要创建用来存储用户的文件,格式为:
abc 123
(前面是用户名,后面是密码)
**文件最后要有~~~TheEnd~~~ ~~~TheEnd~~~ ,否则读取时陷入死循环
下面是读取文件数据的代码
/*
This is the prepare.
*/
int main(){
freopen("Username.in","r",stdin);
map<string,string> login;
login["WilliamWu"] = "Wzh201005140214";
map<string,string>::iterator mit;
mit=login.begin();
mit++;
string a,b;
while(1){
cin>>a>>b;
login.insert({a,b});
if(login.find("~~~TheEnd~~~")!=login.end()) break;
mit++;
}
freopen("CON", "r", stdin);
读取完之后就是主界面了,实现方法为:
string U,P;
int to,todo;
cout<<"欢迎来到登录系统。 现在登录吧!" <<endl;
while(1){
/*里面的具体实现见后文,一定要看完哦!!!*/
}
}
最简单的登录:
cout<<"输入用户名(如果你没有用户名,你可以打 'anymous' 或 'create' 以创建账户)>>>";
cin>>U;
mit=login.find(U);
if(mit!=login.end()){
if(U=="WilliamWu"){
cout<<"欢迎,主人! 现在请您输入密码以验证身份。";
}
else{
cout<<"欢迎,"<<U<<".请输入密码。";
}
cout<<">>>";
while(1){
cin>>P;
if(P==mit->second) {
system("cls");
for(int i=3;i>0;i--){
cout<<"成功登录!"<<endl;
cout<<"我们将在"<<i<<"秒内返回到主页面……" <<endl;
Sleep(1000);
system("cls");
}
break;
}
else if(P=="Quit"){
return 1;
}
else{
cout<<"请重试密码或输入'Quit'以退出!>>>";
}
}
登录后操作:
cout<<U<<",你想做什么?1:退出 2:注销 3:请等待下一个版本!"<<endl;
cout<<"输入数字以选择。"<<endl;
while(1){
cout<<">>>";
cin>>to;
if(to==1) {
freopen("Login.log","w",stdout);
cout<<"Login successful! Username:"<<U;
return 0;
}
else if(to==2){
cout<<"你确定? 1:确定 2:退出";
cin>>todo;
if(todo==2){
continue;
}
cout<<"你确定? 1:确定 2:退出";
cin>>todo;
if(todo==2){
continue;
}
cout<<"你确定? 1:确定 2:退出";
cin>>todo;
if(todo==2){
continue;
}
else if(todo==1&&U!="WilliamWu"){
login.erase(mit);
freopen("login.in","w",stdout);
for(mit=login.begin();mit!=login.end();mit++){
cout<<mit->first<<" ";
cout<<mit->second<<endl;
}
return 0;
}
}
else if(to==3){
cout<<"请等待下一个版本!"<<endl;
}
else continue;
}
}
注册新用户:
else if(U=="create"){
string X;
cout<<"OK,我们来创建一个新账户。";
while(1){
cout<<"输入新账户>>>";
cin>>U;
if(login.find(U)!=login.end()){
cout<<"账户已存在,请登录(L)或创建(C)另一个用户名\n选择 L 或 C>>>";
cin>>X;
if(X=="L"){
break;
}
else continue;
}
else{
cout<<"请输入新密码>>>";
cin>>P;
login.insert({U,P});
freopen("login.in","w",stdout);
for(mit=login.begin();mit!=login.end();mit++){
cout<<mit->first<<" ";
cout<<mit->second<<endl;
}
freopen("CON", "r", stdin);
system("cls");
for(int i=3;i>0;i--){
cout<<"账户已创建完成,请登录。";
cout<<"我们将在"<<i<<"秒内返回到主页面……" <<endl;
Sleep(1000);
system("cls");
}
break;
}
}
continue;
}
佚名登录:
else if(U=="anymous"){
system("cls");
for(int i=3;i>0;i--){
cout<<"Okay,你已登录"<<endl;
cout<<"我们将在"<<i<<"秒内返回到主页面……" <<endl;
Sleep(1000);
system("cls");
}
freopen("Login.log","w",stdout);
cout<<"Login successful! Username:anymous";
return 0;
}
其他情况:
else{
system("cls");
for(int i=3;i>0;i--){
cout<<"我们没找到你的用户名!请登录或创建另一个用户名!"<<endl;
cout<<"我们将在"<<i<<"秒内返回到主页面……" <<endl;
Sleep(1000);
system("cls");
}
continue;
}
}
}
输出结果为:
/*懒得展示,自己复制代码自己看:P:P:P*/
你如果想要英文,我这里也可以提供:
#include<bits/stdc++.h>
#define _CRT_SECURE_NO_WARNINGS
#include<windows.h>
using namespace std;
int main(){
/*
This is the prepare.
*/
freopen("Username.in","r",stdin);
map<string,string> login;
login["WilliamWu"] = "Wzh201005140214";
map<string,string>::iterator mit;
mit=login.begin();
mit++;
string a,b;
while(1){
cin>>a>>b;
login.insert({a,b});
if(login.find("~~~TheEnd~~~")!=login.end()) break;
mit++;
}
freopen("CON", "r", stdin);
/*
This is the main.
*/
string U,P;
int to,todo;
cout<<"Welcome to the login system. Now let's login!" <<endl;
while(1){
cout<<"Input username(If you don't have a username you can type 'anymous' or type 'create' to create a new account.)>>>";
cin>>U;
mit=login.find(U);
if(mit!=login.end()){
if(U=="WilliamWu"){
cout<<"Welcome, my master! Now let's type the passcode in order to verify your information.";
}
else{
cout<<"Welcome, "<<U<<". Please enter your passcode.";
}
cout<<">>>";
while(1){
cin>>P;
if(P==mit->second) {
system("cls");
for(int i=3;i>0;i--){
cout<<"Login successful!"<<endl;
cout<<"We'll return to the main page in "<<i<<" secs." <<endl;
Sleep(1000);
system("cls");
}
break;
}
else if(P=="Quit"){
return 1;
}
else cout<<"Your password is incorrect. Please try again or press 'Quit' to quit!>>>";
}
cout<<U<<", What do you want to do? 1:Quit 2:Delete 3:Please wait for the another version"<<endl;
cout<<"Press the number to choose."<<endl;
while(1){
cout<<">>>";
cin>>to;
if(to==1) {
freopen("Login.log","w",stdout);
cout<<"Login successful! Username:"<<U;
return 0;
}
else if(to==2){
cout<<"Are you sure? 1:Sure 2:quit";
cin>>todo;
if(todo==2){
continue;
}
cout<<"Are you sure? 1:Sure 2:quit";
cin>>todo;
if(todo==2){
continue;
}
cout<<"Are you sure? 1:Sure 2:quit";
cin>>todo;
if(todo==2){
continue;
}
else if(todo==1&&U!="WilliamWu"){
login.erase(mit);
//freopen("login.in","w",stdout);
for(mit=login.begin();mit!=login.end();mit++){
cout<<mit->first<<" ";
cout<<mit->second<<endl;
}
return 0;
}
}
else if(to==3){
cout<<"Please wait for the another version"<<endl;
}
else continue;
}
}
else if(U=="create"){
string X;
cout<<"OK, now let's create a new account.";
while(1){
cout<<"Input your new account>>>";
cin>>U;
if(login.find(U)!=login.end()){
cout<<"The account has already existed, please login(L) or create(C) another account name.\nChoose L or C>>>";
cin>>X;
if(X=="L"){
break;
}
else continue;
}
else{
cout<<"Please enter the passcode for your new account>>>";
cin>>P;
login.insert({U,P});
//freopen("login.in","w",stdout);
for(mit=login.begin();mit!=login.end();mit++){
cout<<mit->first<<" ";
cout<<mit->second<<endl;
cout<<"The new account has being created. Please login.";
break;
}
}
}
continue;
}
else if(U=="anymous"){
cout<<"Okay. Then you have logined."<<endl;
freopen("Login.log","w",stdout);
cout<<"Login successful! Username:anymous";
return 0;
}
else{
cout<<"We didn't find your username! Please create one or type anymous!"<<endl;
continue;
}
}
}
感谢你能认真又耐心地看到这里,我是William,我们下期再见!