我的 for 循环出错,无法弄清楚原因,我有一个"expected declaration"错误
I am having an error with my for loop and can't figure out why, I have an "expected declaration" error
这是一个测试密码强度的程序。我使用了简单的测试来检查密码,但我遇到了问题。让我声明一下,我对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
}
}
我更新了你的代码,它可能编译,也可能不编译,但我修复了我看到的错误。
相关文章:
- 有一个打印语句的函数是一种糟糕的编程实践吗
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 有没有可能有一个只有ADL才能找到的非好友功能
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 在学习数据结构之前对STL有一个了解是好的吗?
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 如何在 Gnuplot 中分别绘制 2 个文件数据?我有一个文件"sin.txt",另一个文件"cos.txt",我想将它们分别绘制在一个图表上
- 是否可以在C++中有一个"generic"模板参数,该参数可以是非类型模板参数或类型?
- 我的输出中有一个额外的 0,为什么会这样
- 节点是否为空,即使它有一个值?
- 将数组信息存储到 c++ 向量中有一个"Access violation reading location"
- 在 Stream C++ 文本之前有一个额外的换行符
- 我可以有一个 ELI5 作为参考和指针以及何时使用它们吗?
- 在 C++ 中声明 const 对象需要用户定义的默认构造函数.如果我有一个可变成员变量,为什么不呢?
- 我有一个类,它创建了另一个类的实例.如何将变量通过第一个类传递到第二个类的实例化中?
- 我有一个线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)错误.我认为这是由于内存管理不好.我可以
- 有一个构造函数,但有两个析构函数
- 我的 for 循环出错,无法弄清楚原因,我有一个"expected declaration"错误