For循环未执行
For-loop not executing
前言:我犯了一个简单的错误,在本该有一个'<'的时候却有了一个'>'。尽管如此,还是有很好的学习经验和小事情如何导致问题的例子,并有奇怪的调试器结果
提前感谢您的帮助。
我正在写一个小程序来检查密码条件。我必须确保密码符合最低要求,即:1)至少六个字符;2) 至少一个大写字母和一个小写字母;3) 至少一个数字。
我的"isUpperCase"函数一直返回一个布尔值false,因为for循环没有执行。在我的调试器中,for循环的索引取了一个我没有分配给它的值。它取了2065172704的值,并且立即失败,即使我正在写:
int i = 0; //It still takes 2065172704, or some other large, random integer as a value.
这篇文章中唯一的函数是"main"、"passwordFailMessage"、"isGreaterThanSix"answers"isUpperCase"。我所有的代码都是我写的。
"isUpperCase"最有可能是问题所在。我认为这是我应该开始进行故障排除的地方,如果修复了,可以指导我在其他函数(如"isLowerCase")中更正解决方案。
此外,"passwordCheck"似乎没有达到返回整数值的程度。
是的,我的程序编译并执行do while循环。我确信还有其他错误,但我希望从"isUpperCase"中for循环失败的原因开始
我正在使用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;
}
打字
for (int i = 0; i > stringLength; i++)
应该是
for (int i = 0; i < stringLength; i++)
相关文章:
- 在映射擦除c++期间执行循环的次数
- C++-For循环未执行
- 未在函数C++中执行的循环
- 为什么切换 for 循环的顺序会显著改变执行时间?
- 将执行循环拆分为多个线程 (1-N-1-N-1..)
- 在类 c++ 中执行 while 循环
- C++ - 使用用户输入的字符串数据检查结构字符串数据(无限执行 while 循环)
- 如何仅在父循环的所有迭代都为真时才执行嵌套的 if 语句?
- 在"0"输入停止循环之前正确执行 while 循环?
- 为什么其中一个 while 循环没有被执行 (C++)?
- 为什么 std::chrono 在测量循环和编译器优化的并行 OpenMP 的执行时间时不起作用?
- 需要帮助转换以执行循环
- 在CIN为真后,使用一段时间循环,然后执行循环
- 为什么要执行循环五次并停止
- 如何为回调执行循环 (C++)
- 执行循环,直到系统时间到达特定日期/时间
- 如何在执行循环(C++)时使间隔为秒
- C++菜单的“执行”循环.测试大写和小写:
- ICC 未执行循环不变代码移动
- 程序不执行循环