Challenge # 1

0 Max Try · August 22, 2014
Hi,
I know this still needs some improvements. But hey! At least, it's working :)

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

int main()
{
    char pw[30];
    int pwlength;
    int i = 0, k;
    int pwCast[30];
    int upperCase = 0, number = 0, symbol = 0;

    printf("Password must be 6 or more characters \n");
    printf("Must contain at least 1 uppercase letter, 1 number and 1 symbol \n");
    printf("Also, please do not include space \n\n");
    do{
    puts("Please enter your password: ");
    gets(pw);

    pwlength = strlen(pw);

    if( pwlength < 6 ){
    printf("Password must be 6 or more characters.\n");

    }
    else{
        for(i=0; i<=pwlength; i++){
            pwCast = pw; //what happened to here and everywhere? Is it just me or I can't really see them when posted?

            if( isalpha(pwCast) ){
                if( isupper(pwCast) ){
                    upperCase = 1;
                }
            }
            else if( isdigit(pwCast)){
                number = 1;
            }
            else if( ispunct(pwCast)){
                symbol = 1;
            }
        }
        if( upperCase == 1 && number == 1 && symbol == 1){
            printf("Great! Your password is %s\n", pw);
            k = 1;
        }
        else{
            //to reset validations
            upperCase = 0;
            number = 0;
            symbol = 0;
            printf("Please try again\n\n");
        }
    }}
    while(!k);
    return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Rolandas Seacret · August 23, 2014
damn it does not show array in if function for some reason. Here is the link for source code: https://dl.dropboxusercontent.com/u/107777491/source.txt   For some reason website does not show in if statement checking array.
0 Max Try · August 22, 2014
Hi, I dunno how it happened but the for loop got messed up here. s were missing. Haha

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

int main()
{
    char pw[30];
    int pwlength;
    int i = 0, k;
    int pwCast[30];
    int upperCase = 0, number = 0, symbol = 0;

    printf("Password must be 6 or more characters \n");
    printf("Must contain at least 1 uppercase letter, 1 number and 1 symbol \n");
    do{
    puts("Please enter your password: ");
    gets(pw); //So it can read space for you password

    pwlength = strlen(pw);

    if( pwlength < 6 ){
    printf("Password must be 6 or more characters.\n");

    }
    else{
        for(i=0; i<=pwlength; i++){
            pwCast = pw; //casting from char array to int array
//so i can use the isalpha, isdigit, ispunct functions
//to find if there are uppercase, symbol, and number in the given password

            if( isalpha(pwCast) ){
                if( isupper(pwCast) ){
                    upperCase = 1; //yup! you can just iterate 1 as long as it won't be 0
                }
            }
            else if( isdigit(pwCast)){
                number = 1; //yup! you can just iterate 1 as long as it won't be 0
            }
            else if( ispunct(pwCast)){
                symbol = 1; //yup! you can just iterate 1 as long as it won't be 0
            }
        }
        if( upperCase == 1 && number == 1 && symbol == 1){
            printf("Great! Your password is %s\n", pw);
            k = 1;
        }
        else{
            //to reset validations
            upperCase = 0;
            number = 0;
            symbol = 0;
            printf("Please try again\n\n");
        }
    }}
    while(!k);
    return 0;
}
0 Rolandas Seacret · August 22, 2014
Here i commented and corrected your program now it works fine :) If you have later any question ask here or pm will be glad to try to help you out ;)

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

int main()
{
char pw[30];
int pwlength;
int i = 0, k;
int pwCast[30];
int upperCase = 0, number = 0, symbol = 0;

printf("Password must be 6 or more characters \n");
printf("Must contain at least 1 uppercase letter, 1 number and 1 symbol \n");
printf("Also, please do not include space \n\n");
do{
puts("Please enter your password: ");
gets(pw);

pwlength = strlen(pw);

if( pwlength < 5 ){   // <5 not 6 if i put 6 characters password you will still get this message
printf("Password must be 6 or more characters.\n");

}
else{
for(i=0; i<=pwlength; i++){
                      //pwCast = pw;   this was your second error like bucky said you can't do this

if( isupper(pw) ){     // your pw was array so let i be its array number dunno how to explain another way if you checking array then in () pass array also
upperCase ++;             //hope you understood dunno how to explain in another way :)
}
else if( isdigit(pw)){
number ++;                 // ++ but not =1 because if number is next char is not a number it will return 0 to you and you will get always false hope you understood :)
}
else if( ispunct(pw)){
symbol ++;
}
}  // here ends loop

if( upperCase > 0 && number > 0 && symbol > 0){  // here i changed to > 0
printf("Great! Your password is %s\n", pw);
k = 1;
}
else{
//to reset validations
upperCase = 0;
number = 0;
symbol = 0;
printf("Please try again\n\n");
}
}}
while(!k);

return 0;
}
0 Rolandas Seacret · August 22, 2014
Yes :) and because of that his program always returns false even if you comment that 30 line :) BTW Bucky thank you for your tutorials ;)
+1 Bucky Roberts · August 22, 2014
Oh I see. The problem with his code is that on line 30...
pwCast = pw;

He is trying to set pwCast (which is an int array) equal to pw (which is a char array). You can't do that because they are different data types. 
0 Rolandas Seacret · August 22, 2014
no i linked you to my version, in image i showed where I'm getting an error when i paste his code to the CodeBlocks :). And when i comment that line of code his program always returns "Please try again" Even if i put upper letter number and symbol
+1 Bucky Roberts · August 22, 2014
That's not the same code as the one in your image. This code runs fine without any errors for me. 

/images/forum/upload/2014-08-22/82d56732532799b2f700194f0d462f6d.png
0 Rolandas Seacret · August 22, 2014
here is my code: https://buckysroom.org/forum/topic.php?id=1387
+1 Bucky Roberts · August 22, 2014
Can you please post your code?
0 Rolandas Seacret · August 22, 2014
Hi, when i copy your code i getting error at line 30. Check it here is the pic: https://dl.dropboxusercontent.com/u/107777491/check.jpg
  • 1

C

129,803 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator