注册 登录
编程论坛 C++教室

我自己写的 登录与注册 的框架 分享一下!!!

xxlovemf 发布于 2010-10-01 15:27, 713 次点击
程序代码:
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;//注册账号(用户名与密码)

int main(int argv,char **argc)
{
    string name,passwd,total;
    ofstream os("d:\data.dat",ios::app);//创建文件 以追加的方式
    string::size_type pos;//子串的位置

    if(!os)
    {
        cerr<<"ofstream error!"<<endl;
        exit(1);
    }

    cout<<"------Register Name------"<<endl;
    cout<<"please input your name:"<<endl;
    cin>>name;
    ifstream is("d:\data.dat");//读取文件
    is >> total;//表示文件中的所有字符串
    pos=total.find(name);//用户名在文件中的位置
      
    if(pos != -1)//若用户名存在
    {
        cout<<"this name is exist!"<<endl;
    }
    else//若用户名不存在
    {
        cout<<"please input your passwd:"<<endl;
        cin>>passwd;
        os<<name<<":"<<passwd<<",";//保存用户名与字符串
        cout<<"------register success------!"<<endl;
    }

    is.close();
    os.close();

    return 0;
}
写得比较简陋,有望大家指出意见!!!!

[ 本帖最后由 xxlovemf 于 2010-10-1 15:30 编辑 ]
1 回复
#2
xxlovemf2010-10-01 15:29
程序代码:
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;//登录账号(用户名与密码)

int main(int argv,char **argc)
{
    string name,passwd,total,test;
    ifstream is("d:\data.dat");//读取文件
    string::size_type pos;//子串的位置

    if(!is)
    {
        cerr<<"ifstream操作错误!"<<endl;
        exit(1);
    }

    cout<<"------Login Name------"<<endl;
    is>>total;
    cout<<"please input your name:"<<endl;
    cin>>name;
    cout<<"please input your passwd:"<<endl;
    cin>>passwd;
    test=name+":"+passwd;//用户名+密码
    pos=total.find(test);//用户名+密码在文件中的位置

    if(pos!=-1)//若用户名存在
    {
        Sleep(3e3);//延时3秒
        cout<<"------login sucsess------!"<<endl;
    }
    else//若用户名不存在
    {
        cout<<"name or passwd is error!"<<endl;
    }

    is.close();

    return 0;
}
1