简体   繁体   English

我有一个错误与我的for循环,不能找出为什么,我有一个“预期声明”错误

[英]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.

相关问题 我的代码有问题,因为我不知道为什么会收到错误。 这是代码: - I'm having issue with my code, in that I can't figure out why I'm receiving an error. Here is the code: 我不知道为什么我的 while 循环没有给出预期的结果): - I can't figure out why my while loop is not giving the expected result ): 我有一个未声明的标识符错误,我无法弄清楚 - I have an undeclared identifier error, that I can't figure out 我得到除以零的错误,但不知道为什么 - I am getting a Divide by zero error but can't figure out why 无法弄清楚为什么我的反向功能变得糟糕 - Can't figure out why I am getting bad out put from my reverse function 编译错误我想不通 - Compile error i can't figure out 我的程序不断抛出编译错误,我无法弄清为什么会发生该错误 - My program keeps throwing compilation error,I am not able to figure out why the error has occured 我在 Qt 上收到错误,无法弄清楚我哪里出错了 - I am getting an error on Qt, and can't figure out where I went wrong 我的教授无法弄清的C ++循环错误 - C++ loop error that my professor can't figure out 我无法弄清楚C ++语法错误“预期`;&#39; 在&#39;{&#39;标记之前 - I can't figure out C++ syntax error “expected `;' before ‘{’ token”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM