[英]Need help passing arrays elements/info to functions C++
I am learning C++ and I am having some trouble using/writing functions. 我正在学习C ++,使用/编写函数时遇到了一些麻烦。
right now I am working on a problem trying to validate a user provided password. 目前,我正在尝试验证用户提供的密码的问题。
The requirements are: 要求是:
1 upper case letter 1 lower case letter 1 number at least 6 characters 1个大写字母1个小写字母1个数字,至少6个字符
each requirment is meant to be validate by passing from main() to a function. 每个需求都应通过从main()传递给函数来进行验证。
I wrote a function for the lower case letter and I keep getting error messages saying there is an unresolved external error. 我为小写字母写了一个函数,并且不断收到错误消息,指出存在未解决的外部错误。 However, when I pass the user_password from the main to the length function it returns the proper statement.
但是,当我将user_password从main传递给length函数时,它将返回正确的语句。
This is the error I keep getting in Visual Studio: 这是我一直在Visual Studio中遇到的错误:
Error 3 error LNK2019: unresolved external symbol "bool __cdecl lowercase(char * const)" (?lowercase@@YA_NQAD@Z) referenced in function _main 错误3错误LNK2019:函数_main中引用的未解析的外部符号“布尔__cdecl小写(char * const)”(?lowercase @@ YA_NQAD @ Z)
Error 4 error LNK1120: 1 unresolved externals 错误4错误LNK1120:1个未解决的外部
Here is the code I have written so far 这是我到目前为止编写的代码
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
bool length(char[]);
bool lowercase(char[]);
int main()
{
const int SIZE = 20;
char user_password[SIZE];
cout << "Please enter a password" << endl;
cout << "**********************************************" << endl;
cout << "Must contain at least 1 upper case letter" << endl;
cout << "Must contain at least 1 lower case letter" << endl;
cout << "Must contain at least 1 number" << endl;
cout << "**********************************************" << endl;
cin.getline(user_password, SIZE);
length(user_password);
lowercase(user_password);
return 0;
}
bool length(char password1[])
{
int length;
length = strlen(password1);
if (length < 6)
{
cout << "Password needs at least 6 characters" << endl;
}
else if (length < 6)
{
cout << "Character requirements met" << endl;
}
return password1;
}
bool lowercase(char password2)
{
if (!islower(password2))
{
cout << "Password requires at least 1 lower letter" << endl;
}
else if (islower(password2))
{
cout << "Lower case letter requirement met" << endl;
}
return password2;
}
This is because the signature on the function definition doesn't match the signature on the declaration. 这是因为函数定义上的签名与声明上的签名不匹配。
bool lowercase(char[]);
bool lowercase(char password2) // OOPS! Doesn't match.
{
....
}
should be 应该
bool lowercase(char password2[])
{
....
}
You should really use either std::string
or at the very least pass by const char
pointer. 您实际上应该使用
std::string
或至少使用const char
指针传递。 Passing by const
pointer is more identifiable with passing a pointer to a null terminated string. 通过将
const
指针传递const
空终止的字符串传递指针,可以更容易识别。
bool lowercase(const char* password2)
Either way you will also need to change how you are calling islower
since it accepts a character value instead of a pointer to a string. 无论哪种方式,您都将需要更改
islower
的调用方式,因为它接受字符值而不是指向字符串的指针。 To to this you will need to iterate over the string in for
or while
loop and check each character individually. 为此,您将需要遍历
for
或while
循环中的字符串,并分别检查每个字符。 You're trying to return a pointer as a boolean value, this won't work. 您尝试将指针返回为布尔值,这将无法正常工作。 Something like this will:
这样的事情会:
bool lowercase(char password2[])
{
for (const char*ptr = password2; *ptr; ++ptr)
{
if (!islower(*ptr))
{
cout << "Password requires at least 1 lower letter" << endl;
return false;
}
}
cout << "Lower case letter requirement met" << endl;
return true;
}
As noted in the comments your implementation of length
is also bonkers. 如评论中所述,您对
length
的实现也很重要。 not only are you checking for the same length twice but you're trying to return a pointer as a boolean value. 您不仅要检查两次相同的长度,而且还要尝试将指针作为布尔值返回。
bool lowercase(char password2[])
{
unsigned int notlower = 0;
for (const char*ptr = password2; *ptr; ++ptr)
{
if (!islower(*ptr))
{
notlower++;
}
}
if (notlower == strlen(password2))
{
cout << "Password requires at least 1 lower letter" << endl;
return false;
}
cout << "Lower case letter requirement met" << endl;
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.