2013-07-25 39 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