首页 > 编程语言 >c++登录注册功能实现代码

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

时间:2023-02-11 15:46:16浏览次数:47  
标签:20 name 登录 c++ char 注册 test password cout

// 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();
}


 

 

 

 

 

 

 

 

 

标签:20,name,登录,c++,char,注册,test,password,cout
From: https://www.cnblogs.com/xiaohao-coding/p/17092480.html

相关文章

  • C++ 地球人口承载力
    题目描述假设地球上的新生资源按恒定速度增长。照此测算,地球上现有资源加上新生资源可供xx亿人生活aa年,或供yy亿人生活bb年。为了能够实现可持续发展,避免资源枯......
  • Redis实战-session共享之修改登录拦截器
    在上一篇中Redis实战之session共享,我们知道了通过Redis实现session共享了,那么token怎么续命呢?怎么刷新用户呢?本来咱们就通过拦截器来实现这两个功能。登录拦截器优化:凯哥......
  • c++ 调用第三orm框架matador的方法通过vs2019
    1.安装matador编译好window版安装包,在安装目录下复制include和lib文件夹到自己的项目目录一下2.自己的mfc目录如图所示,粘贴制include和lib文件夹 3.用vs2019打开自己......
  • HTML_综合注册页面
    <!--该部分为html综合的作业,综合注册页面--><!DOCTYPEhtml><html><head><title>注册界面</title></head><body><formaction=""method=""><ta......
  • 重读vue电商网站4之登录页面总结使用 iconfont 给输入框添加图标
    在 ​​main.js​​​ 中导入字体图标​​​​​​然后在 ​​el-input​​ 组件中通过 ​​prefix-icon​​ 进行相关图标引用​​​​最终效果图如下: ......
  • 重读vue电商网站3之登录页面总结el-form 组件问题
    el-form组件默认是content-box,我们设置其如下属性时,就会出现长度比我们想象中长一点的情况。   .login_form{position:absolute;bottom:60px;......
  • C++语言程序设计课程设计任务书[2023-02-11]
    C++语言程序设计课程设计任务书[2023-02-11]C++语言程序设计课程设计任务书一.课程设计的目的全面系统的学习面向对象程序设计的基本概念、基本语法和编程方法。正确......
  • mysql-proxy 读写分离,不支持mysql5.7的client客户端登录
    安装前,先有mysql主从下载proxy,https://downloads.mysql.com/archives/proxy/#downloadswgethttps://downloads.mysql.com/archives/get/p/21/file/mysql-proxy-0.8.5-......
  • ChatGPT简单注册
    首先来张效果图  下面开始正式步骤特别注意,如果第一次在浏览器上操作,请首先打开代理,然后再去操作,不然会提示你的国家区域不支持请记住操作前先把代理打开,同时了解......
  • C++PrimerPlus中文第六版第10章编程练习答案
    1、//bankaccount.h#ifndefBANKACCOUNT_H_#defineBANKACCOUNT_H_#include<string>classBankAccount{private: std::stringm_account_name; constchar*m_......