InAblender
InAblender

Reputation: 11

Can't figure out why method as argument in another method is repeating

I am new and just started learning C# and there is this challenge where you have to create your own pseudo-login system using methods. The instructor used methods as argument in another method just fine, but when I do it, it is repeating for whatever reason I am unaware of.

I have looked and fiddled for hours now and have been unsuccessful in finding a relevant solution, so I created this account. Please help!

using System; //namespace
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2 
{
    class Program 
    {
        public static void Main(string[] args)
        {
            Login(RegisterName(), RegisterPassword());
            Console.ReadKey();
        }

        public static string RegisterName()
        {
            string userName = "Error. ";  

            Console.WriteLine("Hello, let us begin registration.\n\nPlease enter your desired username. ");
            userName = Console.ReadLine();
            Console.Write("{0} has been registered as your username. ", userName);

            return userName;   
        }

        static string RegisterPassword()
        {
            string userPassword = "Error. ";

            Console.WriteLine("\nNow please enter your desired password. ");
            userPassword = Console.ReadLine();
            Console.Write("{0} has been registered as your password.\n\nAccount registration successful.\n", userPassword);

            return userPassword;  
        }

        static void Login(string _userName, string _userPassword)
        {
            string enteredName;
            string enteredPassword;

            Console.WriteLine("Let us login; first, enter your username. ");
            enteredName = Console.ReadLine();
            Console.WriteLine("Next, your password. ");
            enteredPassword = Console.ReadLine();

            // check
            if (enteredName.Equals( RegisterName() ) && enteredPassword.Equals( RegisterPassword() ) ) 
            {
                Console.WriteLine("Successful login. Welcome {0}. ", enteredName);
            } 
            else 
            {
                Console.WriteLine("Your username or password does not match any known registration. Have you forgotten your details? Please try again or register if you haven't already. ");
            }
        }
    }
}

And this is the output:

Hello, let us begin registration.

Please enter your desired username. 
test 
test has been registered as your username. 
Now please enter your desired password. 
test 
test has been registered as your password.


Account registration successful. 
Let us login; first, enter your username. 

test 
Next, your password. 

test 
Hello, let us begin registration.

Please enter your desired username. 
test 
test has been registered as your username. Now please enter your desired password. 
test 
test has been registered as your password.

Account registration successful. 
Successful login. Welcome test.

(Please forgive the sloppy formatting of the output, and the using stuff that i am not actually using is just stuff left in by the instructor iirc.)

Upvotes: 0

Views: 36

Answers (1)

Brandon Miller
Brandon Miller

Reputation: 1584

if (enteredName.Equals( RegisterName() ) && enteredPassword.Equals( RegisterPassword()) ) {
        Console.WriteLine("Successful login. Welcome {0}. ", enteredName);
    }

This is calling the function twice. What you really want to do here is:

if (enteredName.Equals( _userName ) && enteredPassword.Equals( _userPassword) ) {
        Console.WriteLine("Successful login. Welcome {0}. ", enteredName);
    }

This is a very sloppy way to do something like this, but by the looks of it you're just doing it for learning purposes.

Upvotes: 1

Related Questions