-1

I have a problem with this code. It's our activity in school a while ago and I cant finish it. The problem is if I enter the correct username and password in any of the accounts at the first attempt, the "Invalid Input" is still showing up even though it says "Welcome (username)" then the "Enter Username>> "and I have to enter the username and password again until I finish 3 tries then the code ends(same with second attempt and in the third attempt the "Invalid Input" still shows up). What do I do? What will I add or remove in my code?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        String username;
        String password;
        String[,] accnts = { {"cads123","dadada"},{"carladrian","fafafa"},{"delossantos","gagaga"}};
        int row;

        for (int x = 3; x >= 1; x-- )
        {
            Console.WriteLine("You have "+ x + " attempt/s.");
            Console.Write("Enter Username>> ");
            username = Console.ReadLine();
            Console.Write("Enter Password>> ");
            password = Console.ReadLine();

            for (row = 0; row < 3; row++)
            {
                if (username.Equals(accnts[row,0]) && password.Equals(accnts[row,1]))
                {
                    Console.WriteLine("Welcome "+accnts[row,0]+"!");
                    break;

                }
                else
                {
                    Console.WriteLine("Invalid Input.");
                    if (x != 1)
                    {
                        Console.WriteLine("Please Try Again.");
                        Console.Write("\n");


                    }
                    else if (x.Equals(1))
                    {
                        Console.Write("Goodbye!");
                        break;
                    }

                }


            }
        }
        Console.ReadKey();
    }
}

}

oycarlito
  • 97
  • 1
  • 13
  • possible duplicate of [How to Break out of multiple loops at once in C#?](http://stackoverflow.com/questions/2339142/how-to-break-out-of-multiple-loops-at-once-in-c) – Eugene Podskal Aug 01 '14 at 11:59
  • 5
    When you can't figure out why code that you've written isn't working, the first thing to do is single-step through it using the debugger. Visual C# comes with an *excellent* debugger—use it! – Cody Gray - on strike Aug 01 '14 at 11:59
  • @CodyGray: i know that. but still. I cant finish the code. – oycarlito Aug 01 '14 at 12:01
  • Which username/password are you using? It looks like the code only compares with the first one, not the other two. (Then breaks from the loop after that first one.) It would make more sense to compare with all records at the same time instead of one at a time in a loop like this. – David Aug 01 '14 at 12:01
  • @EugenePodskal: I dont know how to break out of multiple loops. – oycarlito Aug 01 '14 at 12:02
  • 1
    @lyang That's why the following link has been posted here - http://stackoverflow.com/questions/2339142/how-to-break-out-of-multiple-loops-at-once-in-c. It describes several possible methods to solve your problem (your break exits only from the inner loop, but not from the outer one) – Eugene Podskal Aug 01 '14 at 12:06
  • @EugenePodskal: I still cant figure out if I entered the wrong username and password the "Invalid Input" still show up like three times. – oycarlito Aug 01 '14 at 12:25
  • @David: the first one. I cant figure out how will the row increment then try to loop again to find the other two accounts. – oycarlito Aug 01 '14 at 12:26

2 Answers2

1
using System;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            String username;
            String password;
            String[,] accnts = { { "cads123", "dadada" }, { "carladrian", "fafafa" }, { "delossantos", "gagaga" } };
            int row;
            bool isValideUser = false;
            for (int x = 3; x >= 1; x--)
            {
                Console.WriteLine("You have " + x + " attempt/s.");
                Console.Write("Enter Username>> ");
                username = Console.ReadLine();
                Console.Write("Enter Password>> ");
                password = Console.ReadLine();
                for (row = 0; row < 3; row++)
                {
                    if (username.Equals(accnts[row, 0]) && password.Equals(accnts[row, 1]))
                    {
                        Console.WriteLine("Welcome " + accnts[row, 0] + "!");
                        isValideUser = true;
                        break;
                    }
                }
                if (!isValideUser)
                {
                    Console.WriteLine("Invalid Input.");
                    if (x != 1)
                    {
                        Console.WriteLine("Please Try Again.");
                        Console.Write("\n");
                    }
                    else if (x.Equals(1))
                    {
                        Console.Write("Goodbye!");
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
            Console.ReadKey();
        }
    }
}
0

Instead of storing username and password in String[,], you can use Dictionary with Key, Value pair. Key would be username and Value would be password. I can code it here, since as you said it your assignment. Probably you can give it a try. Please try the below code.

            String username;
            String password;
            String[,] accnts = { { "cads123", "dadada" }, { "carladrian", "fafafa" }, { "delossantos", "gagaga" } };
            int row = 0, x = 3;
            bool loggedIn = false;

            do
            {
                Console.WriteLine("You have " + x + " attempt/s.");
                Console.Write("Enter Username>> ");
                username = Console.ReadLine();
                Console.Write("Enter Password>> ");
                password = Console.ReadLine();

                for (row = 0; row < 3; row++)
                {
                    if (username.Equals(accnts[row, 0]) && password.Equals(accnts[row, 1]))
                    {
                        Console.WriteLine("Welcome " + accnts[row, 0] + "!");
                        loggedIn = true;
                        break;
                    }
                }
                if (loggedIn)
                {
                    break;
                }
                else
                {
                    Console.WriteLine("Invalid Input.");
                    if (x != 1)
                    {
                        Console.WriteLine("Please Try Again.");
                        Console.Write("\n");
                    }
                    else if (x.Equals(1))
                    {
                        Console.Write("Goodbye!");
                        break;
                    }
                }                    

            } while (--x > 0);
Sham
  • 830
  • 9
  • 27