0

在函数中定义我的 bool 类型后,我正在为班级分配并不断获得“{”的“预期的不合格 ID”。我无法弄清楚为什么会出现此错误,并且在无法运行程序的情况下很难完成任务。谁能告诉我为什么我会收到这个错误?这是我的代码

//Page 825 Problem 12

#include <iostream>
#include <string>
using namespace std;


//Function Prototype
bool testPassword(char []);

const int passLength = 21;
char password[passLength];

int main()
{
//Ask user to enter password matching the following criteria
cout << "Please enter a password at six characters long. \n"
<< "Password must also contain at least one uppercase and one lowercase letter. \n"
<< "Password must also contain at least one digit. \n"
<< "Please enter your password now \n";

cin.getline (password, passLength);

if (testPassword(password))
    cout << "Password entered is of the correct format and has been accepted.";

else
    cout << "Password does not meet criteria \n";


return 0;
}

//*******************************
//**Function to test password ***
//**to determine if it meets  ***
//**criteria listed           ***
//*******************************

//Test password to determine if it is at least six characters long
bool testPassword (char password[]);


bool lower;
bool upper;
bool digit;
bool length;

 {

    if (strlen(password) < 6)
        length = true;

    else
        length = false;
        cout << "Password must be at least 6 characters long.\n";


for (int k = 0; k < passLength; k++)
{
    if (islower(password[k])
        lower = true;
    else
        lower = false;
        cout << "Password must contain a lowercase letter.\n";


    if (isupper(password[k])
        upper = true;
    else
        upper = false;
        cout << "Password must contain an uppercase letter.\n";


    if (isdigit(password[k])
        digit = true;

    else
        digit = false;
        cout << "Password must contain a digit.\n";

}
if (lower && upper && digit && length == true)

        return true;

    else
        return false;
}
4

3 回答 3

0

听起来你真的想要这个:

bool testPassword (char password[])
{
   bool lower;
   bool upper;
   bool digit;
   bool length;

if (strlen(password) < 6) {
    length = true;
}
else {
    length = false;
    cout << "Password must be at least 6 characters long.\n";
}
...

笔记:

  1. 带有“;”的“testPassword()” 是一个函数原型(不是一个实际的函数定义)

  2. 与 Python 不同,仅缩进不会产生条件块。如果你想在块中多行,你需要花括号。

于 2013-10-06T21:13:24.483 回答
0

testPassword:有一个“;” 以及它不应该有的行尾。

bool 变量需要在第一个“{”内,而不是在它之前。

于 2013-10-06T21:10:16.610 回答
0

这部分放在全局范围内:

bool testPassword (char password[]);   // <-- declaration of function
bool lower;                            // <-- global variables
bool upper;
bool digit;
bool length;
{               // <-- start of the scope? what does it belong to?
    ...

而且它是无效的,你不能将程序的逻辑放在全局范围内......函数不能只是“无处不在”被调用......如果它已经应该是testPassword函数体,它应该是:

bool testPassword (char password[])
{
    bool lower;
    bool upper;
    bool digit;
    bool length;
    ...
}
于 2013-10-06T21:10:24.110 回答