一个简单的垃圾分类程序,但怎么更改密码那个功能总是有bug呀?求帮助
#include<bits/stdc++.h>
#include<time.h>
#include<unistd.h>
#include<conio.h>
#include<windows.h>
using namespace std;
bool cs=false;
int sjbh[4];
int ljtbh;
int space[5]={2000,2000,2000,2000,2000};
int ccrl[4][100000];
long long cch[4][6][100000];
bool judge = false;
int clean=100;
bool function_mm(string password1)
{
int i = 0;
int times = 1;
string s;
char ch;
char pw[50];
while(times<=3)
{
i=0;
while((ch = _getch()) != '\r')
{
if(ch == '\b' && i > 0)
{
printf("\b \b");
i--;
}
else
{
pw[i++] = ch;
cout << '*';
}
}
pw[i] = '\0';
cout << endl;
if(password1.compare(pw) != 0)
{
cout << "密码错误,剩余" << (3-times) << "次机会:";
times++;
}
else
{
cout << "密码正确!垃圾桶已开启。" << endl << endl;
judge=true;
return judge;
}
}
if(times==4){
system("color 46");
for(int i=3;i>=1;i--){
system("cls");
cout<<"警告!警告!警告!\n";
cout<<"您的计算机即将关机!剩余"<<10-i<<"秒";
sleep(1);
}
system("shutdown -s -p");
return judge;
return judge;
}
}
int getRand(int x,int y){
return (rand() % (x- y + 1)) + y;
}
int function_sj(){
srand(time(0));
int r=getRand(1,300);
return r;
}
void function_mx(){
cout<<" __________ __________ __________ __________"<<"\n";
cout<<" |__________| |__________| |__________| |__________|"<<"\n";
cout<<" | | | | | | | | "<<"\n";
cout<<" | | | | | | | |"<<"\n";
cout<<" | 其他垃圾 | |可回收垃圾| | 有害垃圾 | | 厨余垃圾 |"<<"\n";
cout<<" | | | | | | | |"<<"\n";
cout<<" |__________| |__________| |__________| |__________|"<<"\n";
cout<<"查看(1)\n投放(2)\n蓄能(3)\n消毒(4)\n启动(5)\n更改密码(6)\n上锁(7)\n";
}
void function_fl(int x,int y){
int i=function_sj();
if(space[x]-i>=0){
if(x!=y){
do{
cout<<"放错了,请重新放:";
cin>>x;
}while(x!=y);
cout<<"放对了!垃圾重量:"<<i<<"。"<<x<<"号垃圾桶总空间:2000 剩余空间:"<<space[x]-i;
time_t t = time(NULL);
struct tm *localt = localtime(&t);
cch[ljtbh-1][0][sjbh[x-1]]=localt->tm_year+1900;
cch[ljtbh-1][1][sjbh[x-1]]=localt->tm_mon+1;
cch[ljtbh-1][2][sjbh[x-1]]=localt->tm_mday;
cch[ljtbh-1][3][sjbh[x-1]]=localt->tm_hour;
cch[ljtbh-1][4][sjbh[x-1]]=localt->tm_min;
cch[ljtbh-1][5][sjbh[x-1]]=localt->tm_sec;
ccrl[ljtbh-1][sjbh[x-1]]=i;
sjbh[ljtbh-1]++;
switch(x) {
case 1:
space[1]-=i;
break;
case 2:
space[2]-=i;
break;
case 3:
space[3]-=i;
break;
case 4:
space[4]-=i;
break;
}
sleep(2);
system("cls");
}else{
cout<<"放对了!垃圾重量:"<<i<<"。"<<x<<"号垃圾桶总空间:2000 剩余空间:"<<space[x]-i;
time_t t = time(NULL);
struct tm *localt = localtime(&t);
cch[ljtbh-1][0][sjbh[x-1]]=localt->tm_year+1900;
cch[ljtbh-1][1][sjbh[x-1]]=localt->tm_mon+1;
cch[ljtbh-1][2][sjbh[x-1]]=localt->tm_mday;
cch[ljtbh-1][3][sjbh[x-1]]=localt->tm_hour;
cch[ljtbh-1][4][sjbh[x-1]]=localt->tm_min;
cch[ljtbh-1][5][sjbh[x-1]]=localt->tm_sec;
ccrl[ljtbh-1][sjbh[x-1]]=i;
sjbh[ljtbh-1]++;
switch(x) {
case 1:
space[1]-=i;
break;
case 2:
space[2]-=i;
break;
case 3:
space[3]-=i;
break;
case 4:
space[4]-=i;
break;
}
sleep(2);
system("cls");
}
}else{
cout<<"空间不足,请蓄能!";
sleep(2);
system("cls");
}
return;
}
int main(){
string password = "123456";
while(1){
int n=function_sj();
int m;
char sr;
function_mx();
ljtbh=m+1;
cin>>sr;
while(sr<'1'||sr>'7'){
cout<<"输入错误,请重新输入!\n Please type it again!\n请输入:";
cin>>sr;
}
if(sr=='5'){
if(!judge){
cout<<"请输入密码:";
function_mm(password);
sleep(1);
system("cls");
}else{
cout<<"垃圾桶已开启,无需再次输入密码!";
sleep(1);
system("cls");
}
}
if(sr=='2'){
if(judge){
cout<<"请输入垃圾类别:其他垃圾(1),可回收垃圾(2),有害垃圾(3),厨余垃圾(4)。\n请输入:";
cin>>m;
if(m!=1&&m!=2&&m!=3&&m!=4){
while(m!=1&&m!=2&&m!=3&&m!=4){
cout<<"输入错误,请重新输入!\n Please type it again!\n请输入:";
cin>>m;
}
}
cout<<"请输入垃圾桶类别:其他垃圾(1),可回收垃圾(2),有害垃圾(3),厨余垃圾(4)。\n请输入:";
cin>>ljtbh;
function_fl(ljtbh,m);
}else{
cout<<"垃圾桶未开启!";
sleep(1);
system("cls");
}
}
if(sr=='3'){
if(judge){
int snbh;
cout<<"请输入你要蓄能的垃圾桶:其他垃圾(1),可回收垃圾(2),有害垃圾(3),厨余垃圾(4)。\n请输入:";
cin>>snbh;
int nl=space[snbh]/20;
while(nl<=90){
system("cls");
function_mx();
cout<<"正在蓄能中…\n";
cout<<"已完成 ";
nl+=(function_sj())%10;
cout<<nl<<"%";
sleep(1);
}
system("cls");
function_mx();
cout<<"正在蓄能中…\n";
cout<<"已完成 100%";
sleep(1);
system("cls");
function_mx();
cout<<"蓄能完成!\n";
sleep(1);
system("cls");
space[snbh]=2000;
}else{
cout<<"垃圾桶未开启!";
sleep(1);
system("cls");
}
}
if(sr=='1'){
if(judge){
cout<<"请输入你想查看的垃圾桶编号:其他垃圾(1),可回收垃圾(2),有害垃圾(3),厨余垃圾(4)。\n请输入:";
int ckbh;
cin>>ckbh;
if(ckbh<5&&ckbh>0){
for(int j=0;j<sjbh[ckbh-1];j++){
cout<<cch[ckbh-1][0][j]<<'\\'<<cch[ckbh-1][1][j]<<'\\'<<cch[ckbh-1][2][j]<<' ';
if(cch[ckbh-1][3][j]==0)
cout<<"00:";
else if(cch[ckbh-1][3][j]/10)
cout<<cch[ckbh-1][3][j]<<":";
else
cout<<"0"<<cch[ckbh-1][3][j]<<":";
if(cch[ckbh-1][4][j]==0)
cout<<"00:";
else if(cch[ckbh-1][4][j]/10)
cout<<cch[ckbh-1][4][j]<<":";
else
cout<<"0"<<cch[ckbh-1][4][j]<<":";
if(cch[ckbh-1][5][j]==0)
cout<<"00";
else if(cch[ckbh-1][5][j]/10)
cout<<cch[ckbh-1][5][j]<<"\n";
else
cout<<"0"<<cch[ckbh-1][5][j]<<"\n";
cout<<"丢入垃圾重量:"<<ccrl[ckbh-1][j]<<"\n\n";
}
if(sjbh[ckbh-1]==0){
cout<<"暂无记录!";
sleep(1);
system("cls");
}else{
char fh;
cout<<"点击f键返回。\n";
cin>>fh;
if(fh=='f'){
system("cls");
}
}
}
}else{
cout<<"垃圾桶未开启!";
sleep(1);
system("cls");
}
}
if(sr=='4'){
if(judge){
cout<<"正在消毒中…";
for(int i=0;i<100;i++){
cout<<"已完成:"<<i<<"%";
system("cls");
function_mx();
}
}else{
cout<<"垃圾桶未开启!";
sleep(1);
system("cls");
}
}
if(sr=='7'){
if(judge){
judge=false;
cout<<"已上锁!";
}else{
cout<<"垃圾桶未开启!";
sleep(1);
system("cls");
}
}
if(sr=='6'){
if(judge){
cout<<"请输入原密码:";
function_mm(password);
cout<<"请输入新密码:";
cin>>password;
function_mm(password);
cout<<"请再次输入新密码:";
function_mm(password);
}else{
cout<<"垃圾桶未开启!";
sleep(1);
system("cls");
}
}
}
return 0;
}