2013-07-25 42 views
0

我無法讓我的密碼驗證程序正常工作。我的循環似乎只迭代一次,我只是把它作爲輸出,看看它是否不斷迭代,但不是。我不知道爲什麼,布爾運算符正在工作,但它只是迭代一次,如果我的第一個字母是小寫字母,那麼它會說我需要一個大寫字母和一個數字,反之亦然,如果我的第一個字符是數字或大寫。這是一項家庭作業,但我有點失落。任何幫助將不勝感激。字符串驗證字字符循環將無法正常工作

#include<iostream> 
#include<string> 
#include<cctype> 



using namespace std; 


int main() 
{ 
    const int LENGTH = 20; 
    char pass[LENGTH]; 



    cout << "Enter a password, that's at least 6 characters long, one uppercase, one lowercase letter "; 
    cout << " and one digit." << endl; 
    cin.getline(pass,LENGTH); 



    bool isdig = true; 
    bool isdown = true; 
    bool isup = true; 
    bool correct = false; 





    for(int index = 0; correct == false; index++) 
    { 
     cout << "it" << endl; 

     if(isupper(pass[index]) == 0) 
     {isup = false;} 

     if(islower(pass[index]) == 0) 
     {isdown = false;} 

     if(isdigit(pass[index]) == 0) 
     {isdig = false;} 



     if(isdig == true && isup == true && isdown == true) 
     {correct = true;} 



     if(index = LENGTH - 1) 
     { 
      if(isdig == false) 
      {cout << "Your password needs a digit." << endl;} 

      if(isup == false) 
      {cout << "Your password needs an uppercase letter." << endl;} 

      if(isdown == false) 
      {cout << "Your password needs a lowercase letter." << endl;} 

      cout << "Re-enter another password. " << endl; 
      cin.getline(pass,LENGTH); 

      index = 0; 
      isdown = true; 
      isup = true; 
      isdig = true; 
     } 

    } 


    system("pause"); 
    return 0; 

} 
+0

你嘗試過使用調試器?我看到你正在運行MVS ... –

+0

nah我沒有讓我嘗試編輯:ive使用本地Windows調試器在MVS – bigdog225

回答

1

這個問題可能是這一行:

if(index = LENGTH - 1) 

在這裏,您分配LENGTH - 1index價值,所以你總是要求重新輸入密碼爲表達始終是真實的。

0

您應該讓您的編譯器警告(如果使用的是G ++ -Wall),並注意警告:

es.cpp:52:30: warning: suggest parentheses around assignment used as truth value 

這告訴你,有些條件(a==b)作爲可能被寫爲(a=b)這是一個分配。事實上

if(index = LENGTH - 1) 

應該寫

if (index == LENGTH - 1) 

也爲可讀性

if(isdig == true && isup == true && isdown == true) 

可以通過

if (isdig and isup and isdown) 

被替換
if(isdig == false) 

通過

if (not isdig) 
+0

這可能是爲什麼即時通訊運行時錯誤,當我運行它在Microsoft Visual Studio – bigdog225