2011-12-12 210 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輸出您想要的消息。