Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8877649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:30:58+00:00 2026-06-14T19:30:58+00:00

I have the following method to generate a string that has to meet requirements,but

  • 0

I have the following method to generate a string that has to meet requirements,but I need some sort of clause to loop back to user input if a validation requirement is not met.I know in Java there is a subsequent read but I’m not sure what the code required in C is,also I dont think my else if statment are of correct syntax if anyone can spot the error.Some tips or advice would be helpful,
Thanks.

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);
    else if( iResult == gC_FOUND )
    {
        printf("\nPassword used");
    }
    else if
    {
        printf("\nOk to use!");
    }
    else
    {
    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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:30:59+00:00Added an answer on June 14, 2026 at 7:30 pm

    Looks like a case for a do {…} while(…) loop.
    The “else” you are looking for is just after the dictionary lookup.

    Edit:
    It would work like this:

    do {
      /* read password here */
      ...
      if (condition not met) {
        printf("condition not met!\n");
        continue;
      }
      if (another condition not met) {
        printf("another condition not met!\n");
        continue;
      }
      ...
    } while(0);
    

    Edit2:
    It might be a good idea to do the testing in another function like this:

    bool password_is_safe(char *password)
    {
      ...
      if (condition not met) {
        printf("condition not met!\n");
        return false;
      }
      if (another condition not met) {
        printf("another condition not met!\n");
        return false;
      }
      ...
      return true;      
    }
    
      /* in another function */
      ...
      do {
        ...
        /* read password here */
        ...
      } while(!password_is_safe(password));
    

    It makes it much easier to understand the logic of the program without scrolling up and down.

    Edit3:

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    #define STRINGIFY(x) #x
    #define STRINGIFY_VALUE(x) STRINGIFY(x)
    
    #define MAX 80
    
    static bool test_and_complain(const bool test, const char *complain)
    {
        if (test) {
            printf("%s\n", complain);
        }
        return test;
    }
    
    static void write_history(const char *password)
    {
        FILE *f = fopen("passHistory.txt", "w");
        // always check if fopen() was successful
        if (!f) {
            fprintf(stderr, "can't write password history\n");
            exit(EXIT_FAILURE);
        }
        fprintf(f, "%s\n", password);
        fclose(f);
    }
    
    void validatePass()
    {
        char password[MAX+1];
        int iUpper,iLower,iSymbol,iNumber,iTotal;
    
        //shows user password guidelines
        printf("\n\n\t\tPassword rules: ");
        printf("1. Passwords must be at least 9 characters long and less than 15 characters.");
        printf("2. Passwords must have at least 2 numbers in them.");
        printf("3. Passwords must have at least 2 uppercase letters and 2 lowercase letters in them.");
        printf("4. Passwords must have at least 1 symbol in them (eg ?, $, £, %%).");
        printf("5. Passwords may not have small, common words in them eg hat, pow or ate.");
    
        // loop until we got a good password
        bool pw_invalid;
        do {
            //gets user password input
            printf("Enter your password following password rules: ");
            // Security risc:
            // Never ever use "%s" without specifying a length!
            // scanf("%s", password);
            scanf("%" STRINGIFY_VALUE(MAX) "s", password);
    
            countLetters(password,&iUpper,&iLower,&iSymbol,&iNumber,&iTotal);
    
            // Notice that you could eliminate the boolean variable and
            // wrap all the tests in a single expression and put
            // that inside the loop condition of the loop.
            // I think it looks nicer this way though.
            // Notice that the || operator does not evaluate the right hand
            // expression if the left hand expression evaluates to true.
            // I.e. after the first test fails, no other tests are performed.
            // This is equivalent to the if-else cascade from before.
            pw_invalid = false;
            pw_invalid = pw_invalid || test_and_complain(
                          (iUpper < 2),
                          "Not enough uppercase letters!!!");
            pw_invalid = pw_invalid || test_and_complain(
                          (iLower < 2),
                          "Not enough lowercase letters!!!");
            pw_invalid = pw_invalid || test_and_complain(
                          (iSymbol < 1),
                          "Not enough symbols!!!");
            pw_invalid = pw_invalid || test_and_complain(
                          (iNumber < 2),
                          "Not enough numbers!!!") ;
            pw_invalid = pw_invalid || test_and_complain(
                          (iTotal < 9),
                          "Not enough characters!!!");
            pw_invalid = pw_invalid || test_and_complain(
                          (checkWordInFile("dictionary.txt",password)==gC_FOUND),
                          "Found your word in the dictionary");
            pw_invalid = pw_invalid || test_and_complain(
                          (checkWordInFile("passHistory.txt",password)==gC_FOUND),
                          "You already used this password recently");
        } while(pw_invalid);
    
        printf("\nYour new password is verified\n");
        // Security risc:
        // Never ever use a user supplied string as a format string!
        // printf(password);
        printf("%s\n", password);
    
        //writing password to passHistroy file.
        write_history(password);
    
        printf("\n\n\n");
        system("pause");
    } //end validatePass method
    

    If this was homework, please check back with your grade. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method that returns void and I need to use it
I have the following method (used to generate friendly error messages in unit tests):
I have the following method: public string Phase(string phase) { return Phase 1; }
I have a table name Discount that has the following schema: PK DiscountID int
I have a entity that has a scalar string property Clarified. Clarified can have
I have the following code and an input file that has all numbers such
I have the following javascript method: function 123_test_function(){ } The function is generated by
I have following method in wcf webenabled service Public Person AddPerson(Person p); As of
I have following method which I am using to load ActiveX control dynamically, Dim
I have the following method, which takes in the name of a file as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.