2011-12-12 162 views
0

所以这个代码应该验证用户输入的密码。密码必须至少有6个字符,它应该有至少一个大写字母和至少一个小写字符,并且至少应该有一个数字。我试图这样做,当我运行它,它要求我输入密码,但是如果我输入无效密码,它不会显示任何内容。密码验证代码,处理C++ cstrings

#include "stdafx.h" 
#include <iostream> 
#include <cstring> 

using namespace std; 


int _tmain(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){ 

    "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) { 

    "Invalid, the password should contain atleast one uppercase letter"; 

} 

if (times2 == 0) { 

    "Invalid, the password should contain atleast one lowercase letter"; 

} 

if (times3 == 0) { 

    "Invalid, the password should contain atleast one digit"; 

} 



cin.get(); 
return 0; 
} 

回答

5

那是因为你忘了当密码是错误的打印任何东西:

"Not valid, your password should be atleast 6 letters"; 

应该

cout << "Not valid, your password should be atleast 6 letters"; 
+2

哇...典型的3点错误:P haha​​ – user566094

+1

@ user566094,是的,它发生了。 :) –

0

好了,所以你只需要使用std ::法院< <“ “无效,密码应该包含至少一个数字”< < std :: endl输出您想要的消息。