c++登录注册功能实现代码

// ConsoleApplication26.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
//#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE
#include <iostream>
#include<fstream>
#include<conio.h>
#include "ConsoleApplication26.h"

using namespace std;
struct user
{
    string password;
    string name;
};
void home_interface() {
    cout << "hello home" << endl;
}
void regist()
{  //注册
    char* password = new char[20];//分配20个字节,然后返回首地址给password,即password可以看做一个20字节的字符数组,即字符串
    string name;
    /*struct user
      {
         string password;
         string name;
      };定义的用户结构体*/
    user c;
    ifstream fin;//声明 ifstream 对象
    fin.open("user.txr", ios::in);//关联文件
    cout << "请输入用户名:";
    cin >> name;
    while (fin >> c.name >> c.password)//读取成功返回1
    {
        if (c.name == name)
        {
            cout << "该用户名已被注册" << endl; regist();
        }
    }
    char test; int i = 0;
    cout << "输入密码:";
    while ((test = getch()) != '\r')
    {
        if (test == 8)//ASC2码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];
    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 << "密码两次输入不正确";
        regist();
    }
    else
    {
        cout << "注册成功";
    }
    ofstream fout;
    fout.open("user.txt", ios_base::out | ios_base::app);
    fout << name << " " << password << endl;
    fout.close();//关闭文件连接,但不会删除流
    cout << "三秒后返回登陆界面";
    home_interface();//界面
}
void login()
{//登录
    char* password = new char[20];
    string name;
    cout << "用户名:" << endl;
    cin >> name;
    cout << " 密码:" << endl;
    char test; int i = 0;
    while ((test = getch()) != '\r')
    {
        if (test == 8)//ASC2码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("user.txr", ios_base::in);
    if (fin.fail())
    {
        cout << "文件打开失败";
    }
    user c; int f1;
    while (fin >> c.name >> c.password)
    {
        if (c.name == name && c.password == password)
        {
            cout << "登陆成功";
            f1 = 1;//登录成功标志
           home_interface();
        }
    }
    if (f1 == 0)//f1==0登录失败
    {
        cout << "用户名或密码错误";
        login();
    }
}
int main()
{
    login();
}


 

 

 

 

 

 

 

 

 

posted on 2023-02-11 15:34  shenhshihao  阅读(441)  评论(0编辑  收藏  举报

导航