预期结果: 在输入的同时能够倒计时,如果计时结束还未输入完成验证码,则关闭;如果输入正确,则也关闭。(有三次机会)
想法:在主函数调用,但是不能够同时操作
代码如下:
#include<iostream>
#include<time.h>
#include<string>
#include<Windows.h>
#include<conio.h>
using namespace std;
void Getnum(); //得到验证码
void Input(char check_num[]); //输入验证码
void Compare(char give_num[], char give_num_s[]); //判断是否输入正确
void Fobmit(); //禁止输入,并开始倒计时
void CleanScreen(); //清屏
int give_num[4];
char give_num_s[4];
int frequency = 1;
int j = 2;
char check_num[4];
int main(){
srand(time(NULL));
SetConsoleTitleA("验证码");
int start_time = time(0);
Getnum();
Input(check_num);
}
void Getnum() {
if(frequency <=2){
cout << "您收到的验证码是:" << "";
for (int i = 0; i < 1; i++) {
give_num[i] = rand() % 26;
}
for (int i = 1; i < 2; i++) {
give_num[i] = rand() % 26;
}
for (int i = 2; i < 4; i++) {
give_num[i] = rand() % 10;
}
for (int i = 0; i < 1; i++) {
give_num_s[i] = 'A' + give_num[i];
}
for (int i = 1; i < 2; i++) {
give_num_s[i] = 'a' + give_num[i];
}
for (int i = 2; i < 4; i++) {
give_num_s[i] = '0' + give_num[i];
}
for (int i = 0; i < 4; i++) {
cout << give_num_s[i] << " ";
}
}
}
void Input(char check_num[]) {
if(frequency <=3){
cout << "\n" << "\n" << "";
cout << "\n" << "您的输入是:" << "";
for (int i = 0; i < 4; i++) {
cin >> check_num[i];
}
cout << endl;
Compare(check_num,give_num_s);
}
else {
Fobmit();
}
}
void Compare(char check_num[], char give_num_s[]) {
int x;
for (int i = 0; i < 4; i++) {
if (check_num[i] != give_num_s[i]) {
cout <<"\n" << "您输入错了,请重新输入" << "\n";
cout << "\n" << "您还有" << j << "次机会" << endl ;
CleanScreen();
return Input(check_num);
Fobmit();
}
}
cout << "您输入正确" << "";
return;
}
void Fobmit() {
cout << "\n" << "";
cout << "您的机会已经用尽!" << "";
//CountDownTime();
}
void CleanScreen() {
system("pause");
system("cls");
if (frequency <= 3) {
Getnum();
++frequency;
--j;
}
cout << "\n" << "";
}