user5898542 February 2016

How to make number Count in loop in c#?

This is a simple begginer program with setter and getter concept now i have to make it to first enter user name and password to get welcomed and IF I ENTER WRONG INFO IT SHOULD DISPLAY INVALID AND 5 TRIES LEFT THEN if i again enter wrong info it should display 4 tries left and so on and finally when all tries are over it should hang the program or lock the screen or so

using System;


namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            demo obj = new demo();
            string uname, pass;
            Console.ForegroundColor = ConsoleColor.Green;
        label1:
            Console.Clear();
            Console.WriteLine("Enter username");
            uname = Console.ReadLine();
            Console.WriteLine("Enter Password");
            pass = Console.ReadLine();
            obj.setName(uname);
            obj.setPass(pass);
            if (obj.getName() == "niit")
            {
                if (obj.getPass() == "1234")
                {
                    Console.WriteLine("welcome");

                }
            }
            else
            {
                Console.Clear();
                Console.WriteLine("Invalid");
                Console.WriteLine("\n \n \n To try again enter y");
                int n = 5;
                string yes = Console.ReadLine();
                    if (yes == "y")
                {
                    while (n >= 1)
                    {
                        Console.Write(n + " Tries left");
                        goto label1;
                        n = --n;
                    }
                }
            }

            Console.ReadKey();

        }
    }
    class demo
    {
        private string name, pass;
        public void setName(string name)
        {
            this.name = name;
        }
        public string getName()
        {
            return name;
        }
        public void setPass(string pass)
        {
         

Answers


Jhonie February 2016

Everytime you get the wrong input, you remaking your count int n = 5; so everytime you have 5 tries left.

What you can do is to declare your count outside of the static void Main(string args[]) method

just like:

int n =5;
static void Main(string args[])
{

}


Baaleos February 2016

A while loop should suffice. Using a boolean to detect successful password entry. When entered, it will break out of the loop. invalid attempts will decrement the AttemptsLeft int. Note: I haven't tried this in Visual Studio, the logic should be sound, but I recommend debugging and stepping through it to test if it meets your criteria.

static void Main(string[] args)
    {
        demo obj = new demo();
        string uname, pass;
        Console.ForegroundColor = ConsoleColor.Green;
    label1:
        Console.Clear();
        Console.WriteLine("Enter username");
        uname = Console.ReadLine();
        Console.WriteLine("Enter Password");
       bool SuccessfulPassword = false;
       int AttemptsLeft = 5;
        while(!SuccessfulPassword && AttemptsLeft > 0){
        pass = Console.ReadLine();
        obj.setName(uname);
        obj.setPass(pass);
        if (obj.getName() == "niit")
        {
            if (obj.getPass() == "1234")
            {
                Console.WriteLine("welcome");
                SuccessfulPassword = true;
            }
        }
        else
        {
            AttemptsLeft--;
            Console.Clear();
            Console.WriteLine("Invalid");
            Console.WriteLine("\n \n \n To try again enter y");
            int n = 5;
            string yes = Console.ReadLine();
                if (yes == "y")
            {

                    Console.Write(AttemptsLeft + " Tries left");
            }
        }

        Console.ReadKey();
        }
    }


Keerthi Teja February 2016

try this updated main method:

static void Main(string[] args)
        {
            demo obj = new demo();
            int n = 5;
            string uname, pass;
            Console.ForegroundColor = ConsoleColor.Green;
            //Console.Clear();
        label1:
            Console.WriteLine("\n");
            Console.WriteLine("Enter username");
            uname = Console.ReadLine();
            Console.WriteLine("Enter Password");
            pass = Console.ReadLine();
            obj.setName(uname);
            obj.setPass(pass);
            if (obj.getName() == "niit" && obj.getPass() == "1234")
            {
                    Console.WriteLine("welcome");
            }
            else
            {
                //Console.Clear();
                if (n < 1)
                {
                    //Add ur screenlock n hang prog code 
                    Console.Clear();
                    Console.WriteLine("ScreenLock");
                }
                else
                {

                    Console.WriteLine("\n Invalid");
                    Console.WriteLine("\n To try again enter y");
                    string yes = Console.ReadLine();
                    Console.WriteLine("\n");
                    if (yes == "y")
                    {
                        while (n >= 1)
                        {
                            Console.Write(n + " Tries left");
                            n = --n;
                            goto label1;


                        }
                    }
                }
            }

            Console.ReadKey();

        }


AntiHeadshot February 2016

you problems are the following nested if-contitions

if (obj.getName() == "niit")
{
    if (obj.getPass() == "1234")
    {
        Console.WriteLine("welcome");
    }
}
else
{
\\...
}

If the username is correct and the pass not, it wont enter the else branch. a better solution to ask for input until it is valid id a do ... while loop Following example has a lot of improvements over yours.

static void Main(string[] args)
{
    demo obj = new demo();
    string uname, pass;
    Console.ForegroundColor = ConsoleColor.Green;
    int maxTries;
    int tries = maxTries = 5;
    do
    {
        if (tries != maxTries)//second and more
        {
            Console.Clear();
            Console.WriteLine("Invalid");
            Console.Write("\n\t" + tries + " Tries left");
            Console.WriteLine("\n\n\n\tTry again? (y/n)");
            string input;
            do
            {
                input = Console.ReadLine();
            } while (input != "y" && input != "n");

            if (input == "n")
            {
                return; // exit the program
            }
        }

        Console.Clear();
        Console.WriteLine("Enter username");
        uname = Console.ReadLine();
        Console.WriteLine("Enter Password");
        pass = Console.ReadLine();
        obj.setName(uname);
        obj.setPass(pass);
        tries--;
    } while (obj.getName() != "niit" || obj.getPass() != "1234");
    Console.WriteLine("Wellcome");
}

PS: Classes should start with a capital letter.

goto is a relict of old times, it will mess with your programm structure and make things more complicated than they are. The only propper use i know is for fallthrough in switches, which also is needed only in rare cases.

A madeup one would be:

string vehicleType = "car";
switch(vehicleType)
{
    case "truck":
        Console.WriteLine("tw 

Post Status

Asked in February 2016
Viewed 1,247 times
Voted 14
Answered 5 times

Search




Leave an answer