【问题标题】:I have an error in my else..if declaration [closed]我的 else..if 声明中有错误 [关闭]
【发布时间】:2012-11-22 22:22:45
【问题描述】:

我在我的程序中使用了 else..if 语句来检查验证中是否满足某些条件,但我认为我的 else..if 语句的布局有错误,我似乎无法确定.谁能指出我正确的更正或给我一些有用的提示,告诉我在这种情况下我应该怎么做?谢谢。

void validatePass()
{
    FILE *fptr;
    char password[MAX+1];
    int iChar,iUpper,iLower,iSymbol,iNumber,iTotal,iResult,iCount;


    //shows user password guidelines
    printf("\n\n\t\tPassword rules: ");
    printf("\n\n\t\t 1. Passwords must be at least 9 characters long and less than 15 characters. ");
    printf("\n\n\t\t 2. Passwords must have at least 2 numbers in them.");
    printf("\n\n\t\t 3. Passwords must have at least 2 uppercase letters and 2 lowercase letters in them.");
    printf("\n\n\t\t 4. Passwords must have at least 1 symbol in them (eg ?, $, £, %).");
    printf("\n\n\t\t 5. Passwords may not have small, common words in them eg hat, pow or ate.");

    //gets user password input

    printf("\n\n\t\tEnter your password following password rules: ");
    scanf("%s", &password);


    iChar = countLetters(password,&iUpper,&iLower,&iSymbol,&iNumber,&iTotal);

    if(iUpper < 2)
    {
        printf("Not enough uppercase letters!!!\n");

    }

    else if(iLower < 2)
    {
        printf("Not enough lowercase letters!!!\n");

    }
    else if(iSymbol < 1)
    {
        printf("Not enough symbols!!!\n");

    }
    else if(iNumber < 2)
    {
        printf("Not enough numbers!!!\n");

    }
    else if(iTotal < 9 && iTotal > 15)
    {
        printf("Not enough characters!!!\n");

    }

    iResult = checkWordInFile("dictionary.txt",password);

    if( iResult == gC_FOUND )
    {
        printf("\nFound your word in the dictionary");
    }

    else if
    {
        printf("\nCould not find your word in the dictionary");
    }

    iResult = checkWordInFile("passHistory.txt",password);

    if( iResult == gC_FOUND )
    {
        printf("\nPassword used");

    }

    else if
    {
        printf("\nOk to use!");
    }

    printf("\n\n\n Your new password is verified ");
    printf(password);

    //writing password to passHistroy file.


    fptr = fopen("passHistory.txt", "w");   // create or open the file
    for( iCount = 0; iCount < 8; iCount++)
    {
        fprintf(fptr, "%s\n", password[iCount]);
    }

    fclose(fptr);

    printf("\n\n\n");
    system("pause");


}//end validatePass method

【问题讨论】:

  • 您遇到什么错误?哪一行?
  • 这个if(iTotal &lt; 9 &amp;&amp; iTotal &gt; 15) 永远不会评估为真。
  • Paddy,你能建议使用||吗?操作员,或者这在这种情况下是否有效?

标签: c syntax if-statement


【解决方案1】:

没有else if,只有else if () ...。好吧,说得好听一点,你不能在没有条件的情况下拥有if。否则,您只会让编译器想知道“如果是什么?”。

错误

if (...)
{
}
else if
{
    printf("\nCould not find your word in the dictionary");
}

好的

if (...)
{
}
else 
{
    printf("\nCould not find your word in the dictionary");
}

还可以

if (...)
{
}
else if(condition)
{
    printf("\nCould not find your word in the dictionary");
}

【讨论】:

  • 我才意识到如果字典上没有条件,我有 else,干杯
【解决方案2】:

是:

if (exp1)
  action1;

else if (exp2)
  action2;

else
  actionelse;

【讨论】:

    猜你喜欢
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多