2015-11-17 166 views
0

前言:我犯了一個簡單的錯誤,當我有一個'>'時應該有'<'。仍然有很好的學習經驗和很少的事情導致問題的例子,並且具有奇怪的調試結果。For循環未執行

非常感謝您的幫助。

我正在寫一個小程序來檢查密碼標準。我必須確保密碼符合以下最低要求:1)最少6個字符; 2)至少一個大寫字母和一個小寫字母; 3)至少一位數字。

因爲 for循環沒有執行,所以我的「isUpperCase」函數一直返回一個布爾型假值。在我的調試器中,for循環的索引需要一個值,我不分配它。這需要值2065172704,立即失敗,即使我寫:

int i = 0; //It still takes 2065172704, or some other large, random integer as a value. 

在這個崗位的唯一功能是「主」,「passwordFailMessage」,「isGreaterThanSix」和「isUpperCase。」我所有的代碼都是我寫的。

「isUpperCase」最有可能出現問題。我相信這是我應該開始排除故障的地方,並且如果修復的話,可以指導我正確解決其他功能(如「isLowerCase」)的解決方案。

此外,「passwordCheck」似乎沒有達到它返回一個整數值的點。

是的,我的程序編譯和do-while循環執行。我確定還有其他的錯誤,但我希望開始爲什麼我的for循環在「isUpperCase」中失敗。

我正在使用Xcode。

程序如下:

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

using namespace std; 

int MIN_PW_LENGTH = 6; 

int passwordCheck(string); 
bool passwordFailMessage(int); 
bool isGreaterThanSix(string); 
bool isUpperCase(string); 
bool isLowerCase(string); 
bool isDigitInString(string); 

int main() { 
    string password; 

    bool isGood = false; 

    do { 
     cout << "Enter a password at least six characters long,\n"; 
     cout << "with at least one upper case and one lower case letter,\n"; 
     cout << "and at least one digit." << endl; 
     cout << "Password: "; 
     cin >> password; 

     isGood = passwordFailMessage(passwordCheck(password)); 

    } while (!isGood); 


    return 0; 
} 

/******************************************** 
* This function returns integer values, * 
* which represent failure codes, to  * 
* calling function "passwordFailMessage." * 
* It does not seem to return integer  * 
* values because "passwordFailMessage" * 
* does not display a message.    * 
********************************************/ 
int passwordCheck(string s){ 
    int tooShort = 0; 
    int noUpper = 1; 
    int noLower = 2; 
    int noDigit = 3; 
    int isGood = 4; 
    int testMessage; 

    string pword = s; 

    if(!isGreaterThanSix(pword)){ 
     testMessage = tooShort; 
     cout << endl << "Too short." << endl; // To see what is false. 
    } 

    else if (!isUpperCase(pword)){ // <- This function is called, \ 
            // but doesn't execute properly. 
     testMessage = noUpper; 
     cout << endl << "No upper." << endl; // To see what is false. 
    } 

    else if (!isLowerCase(pword)){ 
     testMessage = noLower; 
     cout << endl << "No lower." << endl; // To see what is false. 
    } else if (!isDigitInString(pword)){ 
     testMessage = noDigit; 
     cout << endl << "No digit." << endl; // To see what is false. 
    } else { 
     testMessage = isGood; 
     cout << endl << "Pword good." << endl; // To see what is false. 
    } 

    return testMessage; 
} 

bool passwordFailMessage(int c){ 
    bool isGood = false; 
    int code = c; 

    switch (code) { 
     case 0: // Password too short. 
      cout << "\nThe password is too short, please re-enter." << endl; 
      break; 

     case 1: // No upper case. 
      cout << "\nThe password must have at least one upper case letter." << endl; 
      break; 

     case 2: // No lower case. 
      cout << "\nThe password must have at least one lower case letter." << endl; 
      break; 

     case 3: 
      cout << "\nThe password must have at least one digit 0-9." << endl; 
      break; 

     case 4: 
      cout << "\nPassword accepted." << endl; 
      isGood = true; 
      break; 

     default: 
      break; 
    } 

    return isGood; 
} 

/*********************************** 
* Function below works properly, * 
* testing the passwords is  * 
* greater than, or equal to six. * 
***********************************/ 
bool isGreaterThanSix(string s){ 
    bool isGood = false; 
    string pword = s; 
    int length = static_cast<int>(pword.length()); 

    if (length >= MIN_PW_LENGTH) { 
     isGood = true; 
    } 

    return isGood; 
} 

/*************************************** 
* This function is where I'm having * 
* the most issues. To begin with, it * 
* doesn't seem to enter the for-loop * 
* because 'i' is never true, and I * 
* don't know why. This function is * 
* supposed to check for at least one * 
* upper case letter, and return  * 
* 'true' if there is at least one * 
* upper case letter.     * 
***************************************/ 
bool isUpperCase(string s){ 
    cout << endl << "isUpperCase called" << endl; 
    bool isGood = false; 
    string pword = s; 

    int stringLength = static_cast<int>(pword.length()); 
    cout << "check 2" << endl; // To see if statement executes, this one does. 
    for (int i = 0; i > stringLength; i++) { // Here, 'i' does not take '0' as a value, but other random integer values. 
     cout << "check 3" << endl; // To see statment executes, it isn't for some reason. 
     cout << endl << pword << endl; // To see statment executes, it isn't for some reason. 

     if (isupper(pword.at(i))) { 
      isGood = true; 
      break; 
     } 
    } 

    return isGood; 
} 

回答

2

錯字

for (int i = 0; i > stringLength; i++) 

應該

for (int i = 0; i < stringLength; i++) 
+0

哇...我無法相信我錯過了。或者它在我的調試器中引起了許多奇怪的事情。謝謝 – NonCreature0714