Thread: Multiple functions issues

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    24

    Multiple functions issues

    I think my functions are ok but when interconnected with each it is not doing what I want it to. Any way in how to fix it?
    Code:
    # include <stdio.h>
    #include <stdlib.h>
    
    
    # define MAX_LIMIT 42
    
    
    char VALID_CHARS[6] = {'!', '@', '#', '$', '&', '*'};
    char VOWELS[15] = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U', '0', '1', '3', '!', '@'};
    char CONSECUTIVES_ALLOWED[6] = {'o', 'O', '3', 'e', 'E', '0'};
    
    
    int check_rule_1 (char *password);   //Checks rule 1
    int valid_chars (char c);            //Check for valid characters
    int check_rule_2 (char *password);   // Checks rule 2
    int check_rule_3 (char *password);   //Checks rule 3
    int consecutives_allowed (char c);   //Checks allowed consecutive characters
    int check_rule_4 (char *password);   //Checks rule 4
    int check_vowel (char c);            //Checks for vowels
    int check_rule_5 (char *password);   //Checks rule 5
    int check_valid_char (char c);       //Check for valid characters
    int check_rule_6 (char *password);   //Check rule 6
    int file_end(char *x);
    int one_rule_violated (int *ruleViolated, int x);
    
    
    
    
    int main (){
        FILE *in, *out;
        char password[MAX_LIMIT + 1];     // +1 because of null character at end
        int rules_check[6], i, rules_violated = 0, x = 1;
    
    
        in = fopen("passwords.dat", "r");
        out = fopen("passwords.out", "w");
        
        if (in == NULL || out == NULL)       //Check to see whether files can be openned or not
        {
            printf("Error opening files!\n");
            fclose(in);
            fclose(out);
            return -1;
        }
        
        while (1)
        {
          for(i = 0; i<6;i++)
             rules_check[i]=0;
    
    
          fscanf(in,"%s", password);
    
    
          if(file_end(password)) break;
    
    
          if(check_rule_1(password))
            rules_check[0] = 1;
          else
            rules_violated++;
    
    
          if(check_rule_2(password))
            rules_check[1] = 1;
          else
            rules_violated++;
    
    
          if(check_rule_3(password))
             rules_check[2] = 1;
          else
            rules_violated++;
    
    
          if(check_rule_4(password))
             rules_check[3] = 1;
          else
            rules_violated++;
    
    
          if(check_rule_5(password))
             rules_check[4] = 1;
          else
            rules_violated++;
    
    
          if(check_rule_6(password))
             rules_check[5] = 1;
          else
             rules_violated++;
    
    
          if(rules_violated == 0 )
             fprintf(out,"%s is acceptable.\n", password);
          else if(rules_violated == 1)
             fprintf(out,"%s is not acceptable. Rule %d violated.\n",password,one_rule_violated(rules_check, 6));
          else {
             fprintf(out,"%s is not acceptable. Rules ", password);
    
    
             x = 1;
             for(i = 0;i<6;i++)
                    if(rules_check[i] == 0){
                            fprintf(out, x ? "%d": ",%d", i+1);
                            x = 0;
                    }
             fprintf(out, " violated. \n");
          }
    
    
           rules_violated=0;
        }
    
    
            return 0;
    }
    
    
    int check_rule_1(char *passWord){
    
    
      int length = 0;
    
    
      for(;passWord[length]!='\0';length++);
    
    
      if(length>=5)
            return 1;    //If password is ok return 1 or else return 0
      else
            return 0;
    }    
    
    
    int valid_chars (char c){
        int i;
        for (i = 0; i < 6; i++)
        {
            if(c == VALID_CHARS[i])
               return 1;
            else
               return 0;
            
        }
    }
    
    
    int check_rule_2 (char *password){
        int group[4], i;
    
    
        group[0]=0;   // small case letters
        group[1]=0;   // upper case letters
        group[2]=0;   // numbers 
        group[3]=0;   // special characters
     
        for(i=0;password[i]!='\0';i++){
            
            if(password[i]>='A' && password[i]<='Z')
                    group[0] = 1;
            else if(password[i]>='a' && password[i]<='z')
                    group[1] = 1;
            else if(password[i]>='0' && password[i]<='9')
                    group[2] = 1;
            else if(valid_chars(password[i]))
                    group[3] =1;
        }
    
    
    
    
       if(group[0]+group[1]+group[2]+group[3] >= 3)   //At least 3 groups must be fulfilled
                  return 1;
       else
                     return 0;
    }
    
    
    int check_rule_3 (char *password){
        int i;
    
    
         for(i=0;password[i]!='\0';i++){
            
            if(password[i]>='A' && password[i]<='Z');
            else if(password[i]>='a' && password[i]<='z');
            else if(password[i]>='0' && password[i]<='9');
            else if(valid_chars(password[i]));
            else return 0;
       }
    
    
       return 1;
    }
    
    
    int consecutives_allowed (char c){
        int i;
        for (i = 0; i < 6; i++)
        {
            if (c == CONSECUTIVES_ALLOWED[i])
              return 1;
            else
              return 0;
        }
        
    }
    
    
    int check_rule_4 (char *password){
        int i;
    
    
        for(i=1;password[i]!='\0';i++){
    
    
           if(password[i-1] == password[i]){
    
    
               if(consecutives_allowed(password[i]) == 0)
                      return 0;
           }
        }
    
    
        return 1;
    }
    
    
    int check_vowel (char c){
       int i;
       for (i = 0; i < 15; i++)
       {
          if (c == VOWELS[i])
             return 1;
          else
             return 0;
          
        }
       
    }
    
    
    int check_rule_5 (char *password){
       int i, no_Of_Vowels=0;
    
    
        for(i=0;password[i]!='\0';i++)
            if(check_vowel(password[i]))
                    no_Of_Vowels++;
    
    
       if(no_Of_Vowels>=2)
              return 1;
       else
              return 0;
    }
    
    
    int check_valid_char (char c){
       if(check_vowel(c) == 0){
    
    
            if(c>='A' && c<='Z') return 1;
            else if(c>='a' && c<='z') return 1;
            else if(c>='0' && c<='9') return 1;
            else if(valid_chars(c)) return 1;
            else return 0;
    
    
         }
    
    
         return 0;
    }
    
    
    int check_rule_6 (char *password){
       int i, consecutiveVowels = 0, consecutiveNonVowels = 0;
    
    
        for(i=0;password[i]!='\0';i++){
            if(check_vowel(password[i])){
    
    
                 consecutiveVowels++;
                 consecutiveNonVowels = 0;
             }
            else if(check_valid_char(password[i])){
                  consecutiveNonVowels++;
                  consecutiveVowels = 0;
            }
          
           if(consecutiveVowels == 3 || consecutiveNonVowels == 3)
               return 0;
    
    
        }
    
    
        return 1;
    }
    
    
    int file_end (char *x){
    
    
       int length;
    
    
       for(length=0;x[length]!='\0';length++);
    
    
            if(length!=3)  return 0;
    
    
            if(x[0]=='E' && x[1]=='N' && x[2]=='D')
                    return 1;
            else
                    return 0;
    }
    
    
    int one_rule_violated (int *ruleViolated, int x){
        int i;
    
    
      for(i=0;i<x;i++)
         if(ruleViolated[i]==0)
            return i+1;
    }
    Input file-https://ece15.ucsd.edu/Exams/Problem1/passwords.dat
    Output file-https://ece15.ucsd.edu/Exams/Problem1/passwords.out

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'd start by testing if your functions are ok, i.e., for each function, write and run a unit test to confirm that indeed it works as expected.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regarding to functions and other issues.
    By RyanC in forum C Programming
    Replies: 6
    Last Post: 12-21-2015, 11:54 AM
  2. Issues with functions in C
    By Session in forum C Programming
    Replies: 4
    Last Post: 07-19-2015, 03:56 PM
  3. struct with multiple objects in header file issues
    By Darkroman in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2012, 03:00 PM
  4. Issues compiling multiple c files.
    By Overworked_PhD in forum C Programming
    Replies: 2
    Last Post: 10-11-2009, 04:25 PM

Tags for this Thread