#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, _TCHAR* argv[])
{
const int size = 1000;
char password[size];
int count;
int times1 = 0;
int times2 = 0;
int times3 = 0;
cout << "Please enter your password: ";
cin.getline(password, size);
if (strlen(password) < 6){
cout << "Not valid, your password should be atleast 6 letters";
}
for (count = 0; count < strlen(password); count++)
{
if (isupper(password[count])) {
times1++;
}
if (islower(password[count])){
times2++;
}
if (isdigit(password[count])){
times3++;
}
}
if (times1 == 0) {
cout << "Invalid, the password should contain atleast one uppercase letter";
}
if (times2 == 0) {
cout << "Invalid, the password should contain atleast one lowercase letter";
}
if (times3 == 0) {
cout << "Invalid, the password should contain atleast one digit";
}
cin.get();
return 0;
}
答案 0 :(得分:1)
将所有内容包装在while循环中(从times1 = 0,times2 = 0,times3 = 0到cin.get()之前)。使用名为validPass的bool变量并初始化为true。当其中一个要求失败时,只需make validPass = false。 while应为while(validPass == false){...}
#include "stdafx.h"
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
const int size = 1000;
char password[size];
int count;
bool validPass;
do
{
validPass = true;
int times1 = 0;
int times2 = 0;
int times3 = 0;
cout << "Please enter your password: ";
cin.getline(password, size);
if (strlen(password) < 6){
cout << "Not valid, your password should be atleast 6 letters";
validPass = false;
continue;
}
for (count = 0; count < strlen(password); count++)
{
if (isupper(password[count])) {
times1++;
}
if (islower(password[count])){
times2++;
}
if (isdigit(password[count])){
times3++;
}
}
if (times1 == 0) {
cout << "Invalid, the password should contain atleast one uppercase letter";
validPass = false;
continue;
}
if (times2 == 0) {
cout << "Invalid, the password should contain atleast one lowercase letter";
validPass = false;
continue;
}
if (times3 == 0) {
cout << "Invalid, the password should contain atleast one digit";
validPass = false;
continue;
}
} while (!validPass);
cin.get();
return 0;
}