用户登录注册#include "stdafx.h"#include "iostream"#include "string"#include "stdlib.h"#include "string"#include "conio.h"#include "fstream"#include "windows.h"using namespace std;struct user{string name;string password;}; void nterface(user custum);int main(){void login();void regist();int options;cout << endl << endl << endl << endl;cout << " 1.登录" << endl;cout << " 2.注册" << endl;cout << " (请输入选项并按回车)" << endl;cout << " ";cin >> options;while (options!=1&&options!=2){cout << " erro,请重新输入:"; cin >> options;
}
cout << options;
if (options ==1)
{
system("CLS");
login();
}
if (options == 2)
{
system("CLS");
regist();
system("CLS");
login();
}
return 0;}void login(){string name;char *password = new char[20];cout << endl << endl << endl << endl << endl;cout << " 用户名:";cin >> name;cout << " 密码:";char test='a'; int i = 0;while ((test=_getch())!='\r'){if (test == 8){ if (i > 0)
{
cout << test << " " << test;
password[i--] = '\0';
}
}
else
{
if (i < 20)
{
cout << "*";
password[i] = test;
i++;
}
}
}
password[i] = '\0';
ifstream fin;
fin.open("",ios_base::in);
if (fin.fail())
{
cout << "打开文件失败" <<endl;
}
user custumer;
while (fin >> custumer.name >> custumer.password)
{
if (custumer.name == name&&custumer.password == password)
{
cout << "登录成功!" <<endl;
nterface(custumer);
}
}
if (!(fin >> custumer.name >> custumer.password))
{
}
else
{
cout<<endl << "用户名或密码错误!!!" << endl;
Sleep(3000);
system("CLS");
login();
}
}
void regist()
{
string name;
char *password = new char[20];
cout << endl << endl << endl << endl << endl;
ifstream fin;
fin.open("", ios_base::in);
user custumer;
int i = 0;
cout << " 新建用户名:";
cin >> name;
while (fin >> custumer.name >> custumer.password)
{
if (custumer.name == name) i++;
}
if (i > 0)
{
cout << "该用户名已被注册@!" << endl; regist();
}
cout << " 输入密码:";
char test = 'a'; i = 0;
while ((test = _getch()) != '\r')
{
if (test == 8)
{
if (i > 0)
{
cout << test << " " << test;
password[i--] = '\0';
}
}
else
{
if (i < 20)
{
cout << "*";
password[i] = test;
i++;
}
}
}
password[i] = '\0';
cout << " 再次输入密码:";
char *password2 = new char[20];
test = 'a'; i = 0;
while ((test = _getch()) != '\r')
{
if (test == 8)
{
if (i > 0)
{
cout << test << " " << test;
password2[i--] = '\0';
}
}
else
{
if (i < 20)
{
cout << "*";
password2[i] = test;
i++;
}
}
}
password2[i] = '\0';
if (strcmp(password,password2)!=0)
{
cout <<endl<<" 两次输入密码不一致,请重新注册。。。" << endl;
Sleep(3000);
system("CLS");
regist();
}
else
cout<<endl<< " 注册成功!撸一发庆祝一下";
ofstream fout;
fout.open("",ios_base::out|ios_base::app);
fout << name << " " << password << endl;
fout.close();
cout<<endl<< " 3秒后跳到登录界面...........";
Sleep(3000);
}
void nterface(user custum)
{
system("CLS");
cout << "您好!"<<custum.name << endl<<endl;
}