制作“登录游戏模板”

该博客介绍了一个C++程序,它能将设置的密码和用户名保存到本地,下次运行无需重复输入密码。重点探讨了程序中‘看似没用’的大括号,通过注释括号后报错及查询解答,得出使用两个以上ofstream fout或fin时要加上大括号的结论。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

头文件 

 函数及结构体定义

主函数部分

大括号没用吗

完整代码


先给大家看看效果:

这个程序只有一个特别的:它把设置的密码和用户名保存到本地,下次运行时在后台打开,就不用每次都输入密码。 

 

头文件 

//这是所需要的头文件
#include<bits/stdc++.h>//万能头文件
#include<windows.h>//Windows头文件
using namespace std;//命名空间

 函数及结构体定义

void SetPos(COORD a){//设置光标位置
	HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(out, a);
}
void SetPos(int i, int j){//设置光标位置
	COORD pos={i, j};
	SetPos(pos);
}
struct Load{//开头的加载
	void StartLoad(){//开始加载
		SetPos(50,4); //设置光标位置
		cout<<"----------------------"<<endl;//绘制方框
		SetPos(50,5); 
		cout<<"|                    |"<<endl;//绘制方框
		SetPos(50,6); 
		cout<<"----------------------"<<endl<<endl;//绘制方框
		SetPos(50,7); 
		cout<<"Loading...  0%";//显示进度
	}
	void SetLoadNum(int n){//加载过程
		int t=51;//代表输出方块的位置
		for(int A=0;A<n;A++){
			SetPos(t,5);
			cout<<"■";
			Sleep(500);//休眠0.5秒,呈现“加载”效果
			SetPos(60,7);
			cout<<A*10<<"%   ";//输出对应进度
			t+=2;//计算当前位置
		}
		SetPos(60,7);
		cout<<"100%";
		Sleep(500);
		system("cls");
		return ;
	}
};
void HideCursor(){//隐藏光标函数
	CONSOLE_CURSOR_INFO cursor;    
	cursor.bVisible = FALSE;    
	cursor.dwSize = sizeof(cursor);    
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);    
	SetConsoleCursorInfo(handle, &cursor);
}

主函数部分

int main(){
	HideCursor();//先隐藏光标
	int a;
	string str,str1,str2,str3,str4,str5;
	Load load;//加载
	load.StartLoad();//加载
	load.SetLoadNum(10);//加载
	printf("请按一下Shift键。\n");
	printf("\n你设置过密码吗?(此程序只保留最近一次的密码,如果其他人也设置了密码,请重新设置)(1,0)\n");
	cin>>a;
	if(a==0){
		LLMMGG:
		{
			cout<<"请设置用户名:";
			cin>>str3;
			ofstream fout("yhm.txt");//新建记事本yhm
			fout<<str3;//在文件中输入
			fout.close();//关闭文件
		}
		{
			cout<<"请设置密码:";
			cin>>str;
			ofstream fout("mm.txt");//新建记事本mm
			fout<<str;
			fout.close();
		}
	}
	system("cls");
	system("color f3");
	printf("						登录万能网站\n");
	ifstream fin("yhm.txt");//进入文件
	fin>>str4;//输入文件中的内容
	printf("输入用户名:");
	cin>>str5;
	fin.close();
	if(str5!=str4){
		cout<<"用户名及密码可能已经被删除或代替,5秒后转换到设置用户名界面。";
		Sleep(5000);
		system("cls");
		goto LLMMGG; 
	}
	LLMG:
	{
	ifstream fin("mm.txt");
	fin>>str2;
	printf("输入密码:");
	cin>>str1;
	}
	if(str2==str1)
		printf("登录成功!");
	else{
		cout<<"密码错误。";
		Sleep(2000);
		system("cls");
		goto LLMG;
	}
	int jd,num,geshu,guser,yzm,YZM;
	string psw,PSW,user,USER;
	boop:
	system("color 94");
	system("cls");
	printf("为保证此次登录安全,本次登录需要验证码");
	system("pause");
	srand(time(0));
	YZM=rand()%1000000+1;//生成随机验证码
	printf("验证码:%d",YZM);
	system("pause");
	system("cls");
	printf("输入验证码:");
	scanf("%d",&yzm); 
	if(yzm!=YZM){
		system("color 40"); 
		printf("验证码错误。");
		system("pause"); 
		system("cls");
		goto boop;
	}
	system("color 81");
	cout<<str5;
	printf(",欢迎登录wannengwangzhan.com!");
	return 0;
}

大括号没用吗

 这里要提一下,有些人会发现,我这里(见下图)有几个“看似没用”的大括号,这是为啥捏?

 

 我也是在实际经历中学到的。

请看下面,我把括号注释掉之后:

 

报!错!了! 

我们看看报错信息的意思:

所以,我上网查了查:https://www.baidu.com/s?wd=%5BNote%5D%20%27std%3A%3Aofstream%20fout%27%20previously%20declared%20here&rsv_spt=1&rsv_iqid=0x83c5840100003f7b&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=0&rsv_dl=tb&rsv_n=2&rsv_sug3=3&rsv_t=23ccAqaCKDgZW5KXCVS%2FVoN8vvW46uvPz2JyrVPrvSsEn4eh6bNp5LVWL7KmMDcFeIX7&rsv_btype=i&inputT=5886&rsv_sug4=5887

 再看了看大家的解答,结合自己的想法,就完全明白了。

总之, 使用两个以上的ofstream fout或fin,要加上大括号。

完整代码

#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
void SetPos(COORD a){
	HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(out, a);
}
void SetPos(int i, int j){
	COORD pos={i, j};
	SetPos(pos);
}
struct Load{
	void StartLoad(){
		SetPos(50,4); 
		cout<<"----------------------"<<endl;
		SetPos(50,5); 
		cout<<"|                    |"<<endl;
		SetPos(50,6); 
		cout<<"----------------------"<<endl<<endl;
		SetPos(50,7); 
		cout<<"Loading...  0%";
	}
	void SetLoadNum(int n){
		int t=51;
		for(int A=0;A<n;A++){
			SetPos(t,5);
			cout<<"■";
			Sleep(500);
			SetPos(60,7);
			cout<<A*10<<"%   ";
			t+=2;
		}
		SetPos(60,7);
		cout<<"100%";
		Sleep(500);
		system("cls");
		return ;
	}
};
void HideCursor(){
	CONSOLE_CURSOR_INFO cursor;    
	cursor.bVisible = FALSE;    
	cursor.dwSize = sizeof(cursor);    
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);    
	SetConsoleCursorInfo(handle, &cursor);
}
int main(){
	HideCursor();
	int a;
	string str,str1,str2,str3,str4,str5;
	Load load;
	load.StartLoad();
	load.SetLoadNum(10);
	printf("请按一下Shift键。\n");
	printf("\n你设置过密码吗?(此程序只保留最近一次的密码,如果其他人也设置了密码,请重新设置)(1,0)\n");
	cin>>a;
	if(a==0){
		LLMMGG:
		{
			cout<<"请设置用户名:";
			cin>>str3;
			ofstream fout("yhm.txt");
			fout<<str3;
			fout.close();
		}
		{
			cout<<"请设置密码:";
			cin>>str;
			ofstream fout("mm.txt");
			fout<<str;
			fout.close();
		}
	}
	system("cls");
	system("color f3");
	printf("						登录万能网站\n");
	ifstream fin("yhm.txt");
	fin>>str4;
	printf("输入用户名:");
	cin>>str5;
	fin.close();
	if(str5!=str4){
		cout<<"用户名及密码可能已经被删除或代替,5秒后转换到设置用户名界面。";
		Sleep(5000);
		system("cls");
		goto LLMMGG; 
	}
	LLMG:
	{
	ifstream fin("mm.txt");
	fin>>str2;
	printf("输入密码:");
	cin>>str1;
	}
	if(str2==str1)
		printf("登录成功!");
	else{
		cout<<"密码错误。";
		Sleep(2000);
		system("cls");
		goto LLMG;
	}
	int jd,num,geshu,guser,yzm,YZM;
	string psw,PSW,user,USER;
	boop:
	system("color 94");
	system("cls");
	printf("为保证此次登录安全,本次登录需要验证码");
	system("pause");
	srand(time(0));
	YZM=rand()%1000000+1;
	printf("验证码:%d",YZM);
	system("pause");
	system("cls");
	printf("输入验证码:");
	scanf("%d",&yzm); 
	if(yzm!=YZM){
		system("color 40"); 
		printf("验证码错误。");
		system("pause"); 
		system("cls");
		goto boop;
	}
	system("color 81");
	cout<<str5;
	printf(",欢迎登录wannengwangzhan.com!");
	return 0;
}

好了,文章结束了,觉得好的点个赞!再见!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值