在尝试编译时,使用C ++获得预期的nonqualified-id错误

时间:2013-10-06 21:08:15

标签: c++

我正在为课程分配工作,并在函数中定义我的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;
}

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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;
    ...
}

答案 2 :(得分: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不同,只是缩进不会产生条件块。如果你想在块中有多行,你需要花括号。