Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code is supposed to mimic an account creator and give the user errors when given an improper password (I'll eventually get to Username requirements). I want to loop some code if the user input an improper password but I can't seem to get it down. Any suggestion would be appreciated.

What I have tried:

C#
using System;

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            string Username;
            string Password;
            int Zero;
            int CapCharacters;
            int LowCharacters;
            int Digits;
            int Symbols;
            int PassLength;

            Console.WriteLine("Hello user, please follow these instructions to create your new account.\n");
            Console.Write($"Please enter a username to go by: ");
            Username = Console.ReadLine();

            do
            {
                Zero = 0;
                CapCharacters = 0;
                LowCharacters = 0;
                Digits = 0;
                Symbols = 0;

                Console.Write($"\nAlright {Username}, now please enter a password: ");
                Password = Console.ReadLine(); // Password rules: 6-12 chara, 1 cap, 1 low, 1 num, 1 sym.
                PassLength = Password.Length;
                Console.WriteLine("");

                while (Zero < PassLength)
                {
                    if (Password[Zero] >= 'a' && Password[Zero] <= 'z')
                    {
                        LowCharacters++;
                    }

                    else if (Password[Zero] >= 'A' && Password[Zero] <= 'Z')
                    {
                        CapCharacters++;
                    }

                    else if (Password[Zero] >= '0' && Password[Zero] <= '9')
                    {
                        Digits++;
                    }
                    else
                    {
                        Symbols++;
                    }

                    Zero++;
                }


                if (Password.Length < 6)
                {
                    Console.WriteLine("Error: Your Password is too short.");
                }

                if (Password.Length > 12)
                {
                    Console.WriteLine("Error: Your Password is too long.");
                }

                if (CapCharacters < 1)
                {
                    Console.WriteLine("Error: Your Password needs to contain a capital letter.");
                }

                if (LowCharacters < 1)
                {
                    Console.WriteLine("Error: Your Password needs to contain a lowercase letter.");
                }

                if (Digits < 1)
                {
                    Console.WriteLine("Error: Your Password needs to contain a digit.");
                }

                if (Symbols < 1)
                {
                    Console.WriteLine("Error: Your Password needs to contain a symbol.");
                }
            }

            while (Password.Length < 6 && Password.Length > 12 &&
                   CapCharacters < 1 && LowCharacters < 1 &&
                   Digits < 1 && Symbols < 1);

            Console.WriteLine("");
            Console.WriteLine("Congradulations, your account is now set up!");
            Console.WriteLine("");
        }
    }
}
Posted
Updated 21-Feb-18 11:06am
v2
Comments
j snooze 21-Feb-18 17:17pm    
You need to describe the issue/error you are getting from the current code. Right now I'd say your biggest issue is the while(password.length < 6 && Password.length > 12...)it can't both be shorter than 6 and longer than 12 at the same time. Need to rethink your logic on that one.
F. Xaver 22-Feb-18 5:12am    
why the hell, limit password to max 12 Chars .. so my ubarsecurity 30char pw won't work with your app..
Member 13689794 13-Mar-18 19:56pm    
This is just hypothetical, calm down man. And yes your 30char pw would not work with my app. :)

You need an OR operator in the while condition, not an AND:
C#
while (Password.Length < 6 || Password.Length > 12 ||
       CapCharacters < 1 || LowCharacters < 1 ||
       Digits < 1 || Symbols < 1);

Hope this helps.
 
Share this answer
 
Comments
CPallini 21-Feb-18 17:16pm    
My 5.
Replace
Quote:
while (Password.Length < 6 && Password.Length > 12 &&
CapCharacters < 1 && LowCharacters < 1 &&
Digits < 1 && Symbols < 1);

with
C#
while (Password.Length < 6 || Password.Length > 12 ||
                   CapCharacters || 1 || LowCharacters < 1 ||
                   Digits < 1 || Symbols < 1);


&& operator is logical AND, you know. Now, Password.Length cannot be at the same time less than 6 AND greater than 12.
 
Share this answer
 
Comments
phil.o 21-Feb-18 17:16pm    
5'd :)
Member 13689794 21-Feb-18 17:51pm    
Is there anyway to rephrase the while's condition to be able to use && instead of ||? for instance:
while (Password.Length >= 6 && Password.Length <= 12 &&
CapCharacters > 1 && LowCharacters > 1 &&
Digits > 1 && Symbols > 1);
phil.o 21-Feb-18 17:59pm    
Yes, you can reverse the logic.
In boolean arithmetic, you have:
!(A || B) == !A && !B
!(A && B) == !A || !B

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