Coppertone1234 2022-01-25 10:19 采纳率: 0%
浏览 55

编了一个游戏,支持用户名和密码登录,但是注册之后保存完账号密码到txt里后不会进入游戏界面 C++

使用fileout()保存用户名和密码之后直接return 0;了。

img

//部分代码

#include<iostream>
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<string>
#include<fstream>
#include<conio.h>
#include<cstring>
#include<cstdlib>
#define n
 20
using namespace std;
struct users{
    char name[105];int namelen;
    char password[105];int passwordlen;
    int gold,h,u;
}user[100005];
int usertot,nowuser;
void fileout(){
    freopen("game.txt","w",stdout);
    cout<<usertot<<endl;
    for (int i=1;i<=usertot;i++)cout<<user[i].name<<' '<<user[i].namelen<<' '<<user[i].password<<' '<<user[i].passwordlen<<' '<<user[i].gold<<' '<<user[i].h<<' '<<user[i].u<<endl;
    exit(0);
}
//存档系统 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



    
////////////////////////////////////////////////////////////////////////////////////////////////////
//注册 登录
char rere[105];
bool issame(string x)
{
    for (int i=1;i<x.size();i++)
        if (rere[i]!=x[i])return 0;
    return 1;
}
bool charsame(char a[105],char b[105],int blen)
{
    for (int i=0;i<blen;i++)if (a[i]!=b[i])return 0;
    return 1;
}
int canfind(int len)
{
    int ansi=0;
    for (int i=1;i<=usertot;i++)
        if (charsame(rere,user[i].name,user[i].namelen)&&len==user[i].namelen)ansi=i;
    return ansi;
}
bool login_register()
{
    char k=getch(); 
    while(k!='1'&&k!='2')
    {
        system("cls");
        cout<<"抱歉,请您再试一次"<<endl;
        cout<<"登陆请按1"<<' '<<"用户名"<<' '<<"密码"<<endl;
        cout<<"注册请按2"<<' '<<"用户名"<<' '<<"密码"<<endl;
        k=getch();
    } 
    system("cls");
    if (k=='1'){
        if (usertot==0){cout<<"ERROR"<<endl;return 0;} 
        cout<<"请输入用户名:"<<endl;
        int len;
        scanf("%s",rere);
        len=strlen(rere);   
        system("cls");  
        while(!canfind(len)){
            cout<<"抱歉,未找到用户名"<<endl;
            cout<<"请重新输入"<<endl;
            scanf("%s",rere);
            len=strlen(rere);
            system("cls");
        }
        nowuser=canfind(len);
        cout<<"请输入密码:"<<endl;
        scanf("%s",rere);
        len=strlen(rere);
        system("cls");
        while(!charsame(rere,user[nowuser].password,user[nowuser].passwordlen)||(len!=user[nowuser].passwordlen)){ 
            cout<<"抱歉,密码错误"<<endl;
            cout<<"请重新输入"<<endl;
            scanf("%s",rere);
            len=strlen(rere); 
            system("cls");
        }
    }
    if (k=='2'){
        usertot++;
        nowuser=usertot;
        cout<<"请输入用户名:"<<endl;
        scanf("%s",rere);
        while(canfind(strlen(rere))){
            system("cls");
            cout<<"抱歉,用户名重复,请重新输入"<<endl;
            scanf("%s",rere);
        }
        for (int i=0;i<strlen(rere);i++)user[usertot].name[i]=rere[i];
        user[usertot].namelen=strlen(rere);
        cout<<"请输入密码:"<<endl;
        scanf("%s",user[usertot].password);
        user[usertot].passwordlen=strlen(user[usertot].password);        
        user[usertot].gold=user[usertot].h=user[usertot].u=0;
       
    }
    return 1;
}
//注册 登录 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

//游戏开始 
/*================== the main function ==================*/
int main()
{
    int i = 0;
    cout<< "游戏正在启动中.......\n";
    for (i = 1; i <=100; i++)//首先打印出100个等号,增强进度条的效果
    {
        cout<<"=";
    }
    for (i = 1; i <= 100; i++)
    {
        cout << "\r" << "[" << i << "\%" << "]";//在这一行的首位置打印出进度条百分比
        for (int j = 1; j <= i; j++)
        {
            cout << ">";
            Sleep(0.7);//让程序休眠一会儿,以防进度条过快完成
        }  
    
    }
    cout << string(8, '\n');
    cout << string(50, ' ') << "游戏二次启动中" << endl << ".";
    Sleep(200);
    cout << "\t.";
    Sleep(200);
    cout << "\t\t.";
    Sleep(200);
    cout << "\t\t\t.";
    Sleep(200);
    cout << "\t\t\t.";
    Sleep(200);
    cout << "\t\t.";
    Sleep(200);
    cout << "\t\t.";
    Sleep(200);
    cout << "\t.";
    Sleep(500);
    cout <<  "                              使用条款                    " << endl;
    cout <<  "                                            " << endl;
    cout <<  "                                              " << endl;
    cout <<  "        欢迎使用打飞机游戏                 " << endl;
    cout <<  "        版本2.03春节特别版                 " << endl;
    cout <<  "        作者:苏州市吴江区盛泽第二中学12209水宇辰       " << endl;
    cout <<  "        版权说明:任何单位和个人未经本人书面授权,不得以任何目的          " << endl;
    cout <<  "        修改、使用、复制、截取、上传、下载或以任何方式                " << endl;
    cout <<  "        复制,转载和传播本软件作品的任何部分,否则将视为侵权!        " << endl;
    cout <<  "                                                                             "<<endl;
    Sleep(1500);
    system("cls"); 
    printf("by C++游戏开发集团",50);
    cout<<endl; 
      printf("https://vijos.org/d/Coppertone12345/",50);
    Sleep(500);
    cout<<endl;
       printf("F键打开网页",50);
       cout<<endl;
       printf("按其他键可以进入游戏界面",50);
    char k=getch();
    if(k=='F')
        system("start https://vijos.org/d/Coppertone12345/");
    else
    {
        
        
        //登录 
        freopen("game.txt","r",stdin);
        cin>>usertot;
        for (int i=1;i<=usertot;i++)cin>>user[i].name>>user[i].namelen>>user[i].password>>user[i].passwordlen>>user[i].gold>>user[i].h>>user[i].u;
        cout<<endl; 
        freopen("CON", "r", stdin);
        cout<<"登陆请按1"<<endl;
        cout<<"注册请按2"<<endl;
        string name,password;
        if (!login_register())return 0;
        cout<<"欢迎,"<<user[nowuser].name<<""<<endl;
        Sleep(1500);
        system("cls");
        cout<<"保存中,请稍等..."<<endl;
        system("cls");
        Sleep(100);
        fileout();
        /*
            ofstream outfile;
            outfile.open("game.txt");
            outfile<<user[nowuser].name;
            outfile<<user[nowuser].password<<endl;
            outfile.close();
        */
        system("cls");
       
        //游戏准备
        system("color 07");
        system("cls"); 
        srand((int)time(0));    //随机种子
        HideCursor();   //隐藏光标
        Game game;
        int a = drawMenu();
        if(a == 2)
        game.rank = 20;
        system("cls");
        drawPlaying();
        game.Playing();
         
        
    }
   
}

我尝试换成ofstream outfile;
outfile.open("game.txt");
outfile<<user[nowuser].name;
outfile<<user[nowuser].password<<endl;
outfile.close();
可是提示找不到用户名

我想要达到的结果:成功登陆,注册,且注册后可以进入游戏界面。

  • 写回答

2条回答 默认 最新

  • 小鱼游戏开发 2022-02-03 11:22
    关注

    嗨。你在fileout函数里面调用了exit(0);导致应用退出了
    并不推荐使用freopen重定向stdout。当标准输出stdout被重定向到指定文件后,没有任何方法可以恢复原来的输出流。
    即重定向后你无法在控制台看到任何输出
    建议更换为ifstream或ofstream对文件进行读写

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 1月25日

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R