[英]I am having an error with my for loop and can't figure out why, I have an “expected declaration” error
This is a program that tests for password strength. 这是一个测试密码强度的程序。 I used simple tests to check the password, but I am having issues.
我使用简单的测试来检查密码,但是出现问题。 Let me state that I am not very good with c++, so sorry if the error is apparent
让我说我对C ++不太满意,如果错误很明显,对不起
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <cctype>
using namespace std;
int main()
{
cout << "Please enter your password!" << endl;
cin >> password;
}
bool hasUpp = false;
bool hasLow = false;
bool hasDig = false;
bool hasSym = false;
string strlen, password; //The next line is throwing the error, expecting a declaration
for(int i = 0; < strlen(string1); ++i) //This is where my error is
{
if (isupper(string1[i]))
hasUpp = true;
if (islower(string1[i])) //Do all of these if statements seem correct?
hasLow = true;
if (isdigit(string1[i]))
hasDig = true;
if (issymbol(string1[i]))
hasSym = true;
if (strlen <= 7) //would this be the right way to check for string length?
return false;
if (strlen >= 8)
return true;
}
if (hasLow && hasUpp && hasDig && hasSym)
{
return true;
}
else
{
return false;
}
I am having issues with the for loop towards the top, I can not seem to get rid of this error. 我在for循环的顶部遇到问题,我似乎无法摆脱这个错误。 I don't know what else to write about it, I can't post this without adding more text though so I'm just going to write more words until it let's me post
我不知道还有什么要写的,但是我不能在不添加更多文本的情况下发布它,所以我要写更多的单词,直到让我发布
You've put a bunch of statements (specificaly a for
loop and an if
statement) in global scope. 您已经在全局范围内放置了一堆语句(特别是
for
循环和if
语句)。 Only declarations can appear there (althoug those can contain expressions). 只有声明可以出现在其中(所有声明都可以包含表达式)。
To simplify your code: 为了简化您的代码:
int main()
{
cout << "Please enter your password!" << endl;
cin >> password;
} // the body of main ends here
for (;;) {} // this doesn't belong here
Once you fix that, there's another error: 解决此问题后,就会出现另一个错误:
for(int i = 0; < strlen(string1); ++i)
// ^^^^
// something's missing here
sorry to say but..there are all sorts of things missing in your code here is a more correct version.. try to check the differences.. still somethings may be missing.. but hopefully it puts you on the right track 不好意思,但是..您的代码中有各种各样的东西在这里,这是一个更正确的版本..尝试检查差异..仍然有些东西可能会丢失..但希望它能使您走上正确的轨道
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <cctype>
using namespace std;
int main()
{
string password; //must be declared before use
cout << "Please enter your password!" << endl;
cin >> password; //fetch password
int nlen = strlen(password); //collect the length of text
if (nlen <= 7) //no need to be done inside loop
return false;
if (nlen >= 8)
return true;
bool hasUpp = false;
bool hasLow = false;
bool hasDig = false;
bool hasSym = false;
for(int i = 0; i < nlen ; ++i) //proper iteration
{
if (isupper(password[i]))
hasUpp = true;
if (islower(password[i]))
hasLow = true;
if (isdigit(password[i]))
hasDig = true;
if (issymbol(password[i]))
hasSym = true;
}
if (hasLow && hasUpp && hasDig && hasSym)
{
return true;
}
else
{
return false;
}
}
In this line you declare 2 variables type string: 在这一行中,您声明2个变量,类型为字符串:
string strlen, password;
in this line you are trying to use variable strlen
as a function and put there a variable string1
that does not exist: 在此行中,您尝试将变量
strlen
用作函数,并在其中放置一个不存在的变量string1
:
for(int i = 0; < strlen(string1); ++i)
and you try to use string1
further in the code. 并且您尝试在代码中进一步使用
string1
。
Looks like you copy paste code from somewhere without trying to understand what you are doing. 看起来您是从某个地方复制粘贴代码而没有试图了解您在做什么。
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <cctype>
using namespace std;
// Globals on top
bool hasUpp = false;
bool hasLow = false;
bool hasDig = false;
bool hasSym = false;
// Password Goes Here According to your code
string password;
int main() {
cout << "Please enter your password!" << endl;
cin >> password;
int stringlength = strlen(password);
// check string length here
for(int i = 0; i < stringlength; i++) { // As said, the i was missing, also normally you see i++ in code
if (isupper(string1[i]))
hasUpp = true;
if (islower(string1[i])) //Do all of these if statements seem correct? Yes
hasLow = true;
if (isdigit(string1[i]))
hasDig = true;
if (issymbol(string1[i]))
hasSym = true;
// dont check string length here
}
}
I updated your code, it may or may not compile, but I fixed the errors I saw. 我更新了您的代码,它可能会编译也可能不会编译,但是我更正了我看到的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.