我的 for 循环出错,无法弄清楚原因,我有一个"expected declaration"错误

I am having an error with my for loop and can't figure out why, I have an "expected declaration" error

本文关键字:有一个 expected 错误 declaration 清楚 循环 for 出错 我的 法弄 楚原      更新时间:2023-10-16

这是一个测试密码强度的程序。我使用了简单的测试来检查密码,但我遇到了问题。让我声明一下,我对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;
}

我在向顶部循环时遇到了问题,我似乎无法摆脱这个错误。我不知道还能写什么,我不能在不添加更多文本的情况下发布它,所以我只想写更多的单词,直到它让我发布

您已经在全局范围中放入了一堆语句(特别是for循环和if语句)。只有声明可以出现在那里(尽管这些声明可以包含表达式)。

简化代码:

int main()
{
    cout << "Please enter your password!" << endl;
    cin >> password;
}  // the body of main ends here
for (;;) {} // this doesn't belong here

一旦你解决了这个问题,就会出现另一个错误:

for(int i = 0;      < strlen(string1); ++i)
//             ^^^^
//             something's missing here

很抱歉,但是。。你的代码中缺少了各种各样的东西,这是一个更正确的版本。。试着检查一下差异。。还有一些东西可能不见了。。但希望它能让你走上正确的轨道

#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;
}

}

在这一行中,您声明了两个变量类型字符串:

string strlen, password;

在这一行中,您试图使用变量strlen作为函数,并在其中放置一个不存在的变量string1

for(int i = 0; < strlen(string1); ++i)

并尝试在代码中进一步使用CCD_ 5。

看起来你从某个地方复制粘贴代码,却没有试图理解你在做什么。

#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
    }
}

我更新了你的代码,它可能编译,也可能不编译,但我修复了我看到的错误。

相关文章: