Challenge 1 (Mostly completed)

0 Daniel Seflinger · October 26, 2014

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
int main(){ //user enters a password with Upper case letter, a number, and a $ sign
char password[20];
int i, dig = 0, up = 0, dollar = 0;
printf("Make a up a password with an Upper case letter, a number and a dollar sign: ");
scanf(" %s", &password);

for (i = 0; i < 20; i++){
if (isdigit(password))
dig++;
else if (isupper(password))
up++;
else if (password == '$')
dollar++;
}
if ((dig >= 1) && (up >= 1) && (dollar >= 1))
printf("Your password: %s is valid\n", password);
else
printf("NOT A VALID PASSWORD\n");

return 0;
}


Can someone please explain to me why my code isn't working? Or am I crazy.

Post a Reply

Replies

Oldest  Newest  Rating
+1 Daniel Seflinger · November 3, 2014
Wow thank you guys so much! Yes i realized that my program wasn't working because it was reading the extra unnecessary characters that my array could use. I forgot to add the strlen(password) in my loop, but once I did the program worked. 
+1 Daniel Seflinger · November 3, 2014
I'm pretty sure he only said at least one $ sign, one capital letter, and one number. So no, we didn't need any other symbols
0 Homer Simpson · November 1, 2014
So here's the first thing I did. I ran your code. Didn't work, next thing I did was add this line of code at the bottom.

printf("Digits:%d\nUppercase: %d\nSymbol: %d", dig, up, dollar);

This will print out the total amount of uppercase letters, symbols, and digits. After running it again my result for each was 0, so since the for loop is supposed to go through and increment those and it's not doing so. That must be where my problem is. Here was my fix.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
int main(){ //user enters a password with Upper case letter, a number, and a $ sign
char password[20];
int i, dig = 0, up = 0, dollar = 0;
printf("Make a up a password with an Upper case letter, a number and a dollar sign: \n");
scanf(" %s", &password);

for (i = 0; i < 20; i++){
if (isdigit(password))
dig++;
else if (isupper(password))
up++;
else if (password == '$')
dollar++;
}
if ((dig >= 1) && (up >= 1) && (dollar >= 1))
printf("Your password: %s is valid\n", password);
else
printf("NOT A VALID PASSWORD\n");

printf("Digits:%d\nUppercase: %d\nSymbol: %d", dig, up, dollar);

return 0;
}

i added after password so that it will go through each char scanning it individually.

I hope this made sense. If you need me to clarify anything just let me know.
0 Homer Simpson · November 1, 2014
What the hell, lol I hadnt noticed that this site removed the . Thats crazy. Thanks for the heads up. :D
I got the code to work though.
0 Daniel Vladimirov · November 1, 2014
The problem with this program is that  as we can obviously see the only symbol it accepts is $.

Fixing it, leaving all the symbols under the else statement though gives us another problem:
  The unused characters from the array are counted as symbols and even if we haven't included any symbols, if we haven't used the whole array's length the password is going to be accepted because of the empty spaces.(PM me to explain you what I meant)
  To solve the problem we need to find a way for the program to understand how many characters we have included so that it doesn't loop unnecessarily and in this way count phantom symbols.
0 Youknees Adoro · November 2, 2014
I got these codes working

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
//#include <math.h>
#include <string.h>
int main(){ //user enters a password with Upper case letter, a number, and a $ sign
char password[20];
int i, dig = 0, up = 0, dollar = 0;
printf("Make a up a password with an Upper case letter, a number and a dollar sign: \n");
scanf(" %s", &password);

for (i = 0; i < strlen(password); i++){
if (isdigit(password))
dig++;
else if (isupper(password))
up++;
else if (password == '$')
dollar++;
}
if ((dig >= 1) && (up >= 1) && (dollar >= 1))
printf("Your password: %s is valid\n", password);
else
printf("\nNOT A VALID PASSWORD\n");

printf("\nDigits:%d\nUppercase: %d\nSymbol: %d", dig, up, dollar);

return 0;
}
0 Youknees Adoro · November 2, 2014
/images/forum/upload/2014-11-02/09d79fbc3f0e32d2f06f9d3c95235e40.PNG
0 Daniel Vladimirov · November 2, 2014
Yes, that works indeed but how are you going to make a program that allows you to use whatever symbols you want and not just $?
0 Youknees Adoro · November 3, 2014
ohhh, is it needed that it should accept any special characters not only the dollar sign?
  • 1

C

107,100 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator