Thread: Debug Assertion Failed!

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    2

    Debug Assertion Failed!

    In an attempt to familiarize myself with the fundamentals of C, I've been trying to write a simple program for choosing a password.

    The password is suppose to conform to the 5 listed conditions. The code is designed to loop through the password to determine if the conditions are satisfied and prompt users of any issues. If the conditions are satisfied its coinciding variable is set to 1. Any variable left 0 is intended to prompt an invalid password.

    However anytime the password fails condition 2-5 my program will crash with the following prompt from the compiler.

    Debug Assertion Failed!-untitled_zps7rt6mjwx-png
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <math.h>
    int main(void) {
        char password[21];
        int loop = 0;
        int dollar = 0;
        int digit = 0;
        int upperCase = 0;
        int lowerCase = 0;
        printf("Requirements for a valid password:nn");
        printf("1. Contain at least one $ sign.n");
        printf("2. Contain at least one number.n");
        printf("3. Contain at least one uppercase letter.n");
        printf("4. Contain at least one lowercase letter.n");
        printf("5. Contain no more than 20 characters.nn");
        printf("Enter password: ");
        scanf(" %s", password);
        printf("n");
        for (loop = 0; loop < 21; loop++) {
        if (password[loop] == '$') {
        dollar = 1;
        break;
        }
        }
        if (dollar == 0) {
        printf("Invalid password, recheck for condition 1.nn");
        }
        for (loop = 0; loop < 21; loop++) {
        if (isdigit (password[loop])) {
        digit = 1;
        break;
        }
        }
        if (digit == 0){
        printf("Invalid password, recheck for condition 2.nn");
        }
        for (loop = 0; loop < 21; loop++) {
        if (isupper(password[loop])) {
        upperCase = 1;
        break;
        }
        }
        if (upperCase == 0) {
        printf("Invalid password, recheck for condition 2.nn");
        }
        for (loop = 0; loop < 21; loop++) {
        if (islower(password[loop])) {
        lowerCase = 1;
        break;
        }
        }
        if (lowerCase == 0) {
        printf("Invalid password, recheck for condition 2.nn");
        }
        if ((dollar * digit * upperCase * lowerCase) != 0) {
        printf("Password saved!nn");
        }
        system("pause")
        return(0);
    }

  2. #2
    Registered User john.c's Avatar
    Join Date
    Dec 2017
    Posts
    316
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    int main() {
        char password[100];
        int dollar = 0;
        int digit = 0;
        int upperCase = 0;
        int lowerCase = 0;
    
        printf("Requirements for a valid password:\n");
        printf("1. Contains at least one $ sign.\n");
        printf("2. Contains at least one number.\n");
        printf("3. Contains at least one uppercase letter.\n");
        printf("4. Contains at least one lowercase letter.\n");
        printf("5. Contains no more than 20 characters.\n\n");
    
        printf("Enter password: ");
        scanf(" %99s", password);
    
        int len = strlen(password);
    
        for (int i = 0; i < len; i++) {
            if (password[i] == '$')
                dollar = 1;
            else if (isdigit(password[i]))
                digit = 1;
            else if (isupper(password[i]))
                upperCase = 1;
            else if (islower(password[i]))
                lowerCase = 1;
        }
    
        if (!dollar)
            printf("Invalid password, recheck for condition 1.\n");
        if (!digit)
            printf("Invalid password, recheck for condition 2.\n");
        if (!upperCase)
            printf("Invalid password, recheck for condition 3.\n");
        if (!lowerCase)
            printf("Invalid password, recheck for condition 4.\n");
    
        if (len > 20)
            printf("Invalid password, recheck for condition 5.\n");
        else if (dollar && digit && upperCase && lowerCase)
            printf("Password saved.\n");
    
        return 0;
    }
    Simplicity is the ultimate sophistication.

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    2
    Thank you for your reply,

    I actually made the initial code work by changing the following;

    Code:
    for (loop = 0; password[loop]; loop++)
    However, I greatly appreciate your input though. Definitely, demonstrates a cleaner method.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug Assertion Failed!
    By IndioDoido in forum C Programming
    Replies: 31
    Last Post: 03-25-2008, 11:07 AM
  2. debug assertion failed
    By adrianchick in forum C++ Programming
    Replies: 1
    Last Post: 09-21-2005, 02:59 PM
  3. debug assertion failed !
    By blue_gene in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2004, 11:23 AM
  4. Debug Assertion Failed!
    By Ray Schmidt in forum C++ Programming
    Replies: 3
    Last Post: 02-21-2003, 09:58 PM
  5. Debug Assertion Failed
    By minesweeper in forum Windows Programming
    Replies: 5
    Last Post: 12-11-2002, 05:11 PM

Tags for this Thread