目录
先给大家看看效果:
这个程序只有一个特别的:它把设置的密码和用户名保存到本地,下次运行时在后台打开,就不用每次都输入密码。
头文件
//这是所需要的头文件
#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;
}
大括号没用吗
这里要提一下,有些人会发现,我这里(见下图)有几个“看似没用”的大括号,这是为啥捏?
我也是在实际经历中学到的。
请看下面,我把括号注释掉之后:
报!错!了!
我们看看报错信息的意思:
再看了看大家的解答,结合自己的想法,就完全明白了。
总之, 使用两个以上的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;
}
好了,文章结束了,觉得好的点个赞!再见!