0

Im trying to write a program that takes a password created by a user and compares it to an array of strings. The password has to be a word not in the array. For example: The user enters the password "about" and that word is in the array so it will return "Wrong". What i've tried dosent work, Can someone help?

Here's everything i have so far:

int main()

{
    int i;
    char dict[998][15];
    char password[30];
    size_t ind; 

    FILE *fp = fopen("dictionary.txt", "r");

    if (fp != NULL) 
    {
      
        for (ind = 0; fgets(dict[ind], sizeof dict[0], fp) && ind < sizeof dict / sizeof dict[0]; ind++)
        {
            dict[ind][strcspn(dict[ind], "\n")] = '\0'; 
        }

  
    }
    else
    {
        printf("Error opening file");
    }
    fclose(fp);
     printf("Please enter Password: ");
     scanf("%s",password);
    //Checking if password is in array.
       for (size_t i = 0; i < ind; i++) 
       {
             if (!strcmp(password, dict[i])) 
             {
                 printf("Wrong");
             }
             else
             {
                 printf("Correct");
             }
       }
   
}
2
  • Define "dosent work". Commented Mar 28, 2021 at 23:10
  • @user14063792468 It returns Correct 998 times Commented Mar 28, 2021 at 23:12

1 Answer 1

1

You are writing "Correct" or "Wrong" for every checked word in the dictionary. You have to stop checking if the word appears in the dictionary, and if all words are checked and the password does not appear, then print "Correct".

A possible implementation that just prints the result and exits would be something like:

printf("Please enter Password: ");
scanf("%s", password);
//Checking if password is in array.
for (size_t i = 0; i < ind; i++)
{
    if (!strcmp(password, dict[i]))
    {
        printf("Wrong");
        return -1;
    }
}
printf("Correct");
return 0;
Sign up to request clarification or add additional context in comments.

2 Comments

This stops the printing issue, but showed me a new one. The loop isn't checking all of the strings. For example the first string in the array is "about" and the last is "zoo". When i enter the password as "about" it gives me "Correct". But when i enter the password as "zoo" it says "Wrong" when both should say wrong.
I think that maybe your dictionary is wrong (some white space after about?). I would try to reduce the dictionary just to those two entries (about and zoo) and check if the error is reproduced. In that case, I would debug the program and check what happens in the comparison. If you are not used to debuggers (you should start playing with them), just add some printf just before the strcmp to see if what you are comparing is the same. For instance: printf ("Comparing '%s' and '%s'\n", password, dict[i]); Don't forget the quotes because it makes detecting white spaces easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.