Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Coders and Codesses!

Alright, so I finally have a working program after making many adjustments...BUT when I run it, it somehow manages to pick up characters that are not even in the user input!

This is my code:

C++
#include <stdio.h>
#include <ctype.h>

int main()
{
    int i;
    char password[50];
    int upper = 0;
    int number = 0;
    int dollarSign = 0;
    
//ask the user to input a password containing an uppercase letter and number and $ sign

    printf("Please enter a password containing an Uppercase letter, a number and a $ sign: ");
    scanf(" %s", password);
    
//now we check if the password given contains an uppercase letter, a number and a $ sign

    for( i = 0; i < 50; i++)
    {
        if ( isalpha(password[i]))
        {
            if( isupper(password[i]))
            {
            upper += 1;
            printf("Your password contains an Uppercase letter: %c\n", password[i]);
            break;
            }
        }
    }
    
    for( i = 0; i < 50; i++)
    {
        if ( isdigit(password[i]))
        {
            number += 1;
            printf("Your password contains a number: %c\n", password[i]);
            break;
        }
    }
    
    for( i = 0; i < 50; i++)
    {   
        if (password[i] == '$')
        {
            dollarSign += 1;
            printf("Your password contains a dollar sign: %c\n\n", password[i]);
            break;
        }
    }
    
//now we give the user final feedback on whether their password is acceptable or not

    if ( (upper == 1)  && (number == 1) && (dollarSign == 1) )
    {
        printf("You have a strong password!\n");
    }
    else
    {
        printf("Your password sucks!\n");
    }
  
    return 0;
}



(Old part of question...please ignore

I'm a newbie and I have the following problem challenge:
Build a program where the user enters a password and program scans it checking for an uppercase letter, a number, and a '$' sign.

Because I am so new I really have no idea how to fix this problem and any help would be GREATLY appreciated :)

This is my code:
C++
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main()
{
    char password[50];
    int i;
    int goodPassword = 0;

    printf("Enter a password containing an Uppercase letter, a number and a $ sign\n");
    scanf(" %s", password);

    for(i=0; i<=50; i++){
        if(isupper(password[i])) && (isdigit(password[i])) && (password[i] == '$'){
            goodPassword = 1;
            printf("Good to go!");
            break;
        }else{
            printf("It sucks!");
        }
        }
        return 0;
}
)

What I have tried:

So the first user password input I ran was: eHello$1 

This worked perfectly!

Then I ran tested a user input of: iamsam

Please enter a password containing an Uppercase letter, a number and a $ sign: iamsam
Your password contains an Uppercase letter: L
Your password sucks!

Where does it get the 'L' from? lol

and one more attempt with just a single letter: i

<pre>Please enter a password containing an Uppercase letter, a number and a $ sign: i
Your password contains a number: 0
Your password sucks!

It now tells me the password contains a 0.

Please PLEASE help! Everyone was so amazing helping me fix my first attempt of this problem.
Posted
Updated 4-Sep-20 6:59am
v4
Comments
jeron1 4-Sep-20 12:34pm    
Have you tried initializing the values of your password array before using?
Richard MacCutchan 4-Sep-20 12:36pm    
That is not necessary if you use strlen to find out how many characters were actually entered.
Richard MacCutchan 4-Sep-20 12:36pm    
You are checking all 50 character positions of your password array. What happens if the user enters fewer characters? You need to use the length of the entered data (from strlen) to know how many characters to test.
BooleanBabe 4-Sep-20 14:06pm    
Thanks Richard! This helped tremendously.

&& is an AND operation, so what you are saying is "if a is 1 and a is also 2 and a is also 3, it's a good password" - which requires "a" to have three different values, which is silly! :)

Think about how you would do it manually: you'd count the number of uppercase, the number of digits, and the umber of special characters. Then you'd check the totals at the end. So do the same thing in your code:

Set up three integers: upperCount, dollarCount, digitCount.
Then in your loop check each character up to three times:
If it's Uppercase, increment upperCount.
Otherwise, if it's a Digit, increment digitCount.
Otherwise, if it's a dollar sign, increment dollarCoun.
Otherwise, do nothing.
After the loop, check the three counts: if they are all greater than zero, it's good.
Otherwise, it's too weak.
 
Share this answer
 
Comments
BooleanBabe 30-Aug-20 9:27am    
Thank you for the suggestion! I must be honest...still trying to figure it out, but your guidance has been of great assistance. At least my code is compiling now. lol
OriginalGriff 30-Aug-20 9:32am    
You're welcome!
Like 'Griff said, your tests in the your if statement can only ever be true if the character you're testing is holding 3 different values at the same time. That's not possible.

I'll give you a hint. Run every character through a series of tests and if it passes a test, set a flag for that test. See where this is going?
 
Share this answer
 
v2
Comments
BooleanBabe 30-Aug-20 9:28am    
I see where this is going. I think I am struggling with this because I have not yet figured out how to run through an array...and I think that is the key to the problem.
One other thing - you don't need to check 50 characters of the string unless that is how long it is. Your loop should be something like this :
C++
for( i=0; password[i] != 0; ++i )
 
Share this answer
 
Comments
BooleanBabe 30-Aug-20 9:29am    
Ok...will keep this in mind. Thank you. :)
Quote:
Why does my password program find characters that does not exist in the user input?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Password testing is an art, the primary checks are absolut necessary, but a good password checks checks also for word phrases and pattern like Qwert123.

Today is also needed to require a special character non digit or character (a-z, A-Z) like !#* and so on.
 
Share this answer
 
Comments
BooleanBabe 30-Aug-20 9:29am    
Thanks for the suggestion! Much appreciated!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900