none
username and password?

    Question

  • hi,

    i am trying to make a c# console-app, to ask for a user and a pass, by using the if-else statemets, to be able to proceeed?

    how can i do this?

    i have written this code below so far, but is dosnt work?:

    double hamedeus, admin, user, pass;



    Console.Write("\n\nUsername: ");

    user = Double.Parse(Console.ReadLine());

    Console.Write("\n\nPassword: ");

    pass = Double.Parse(Console.ReadLine());

    if ((user == hamedeus) && (pass == admin))

    else

    Console.WriteLine("You have entered the wrong username or password!");

    Console.ReadLine();

    Monday, August 18, 2008 6:59 PM

Answers

  • A double is a 64 bit floating point number, what you want to use are strings (string user, pass). Then you'll want something like:

    if (user != "hamadeus" || pass != "admin")
    {
        Console.WriteLine("You have entered the wrong username or password!");
        // exit program here
    }

    // continue program here

    But there are still many problems with your program. For one, it's not going to be very secure unless you're just trying to keep your little brother away from it. Anyone can use easy to find tools to look at your source code and get the username and password you've set in code because it's not encrypted. Second, you need to read up more on programming, readsome tutorials. When you define hamedeus and admin at the top, you are defining variables with those names, the strings don't actually have that value. The value is either "" (an empty string) or you might receive an error because you're trying to compare them before you give them a value. The code I have given you shows you how to compare the variables where the user input is stored with the actual strings "hamadeus" and "admin".

    So really you need to do some more reading on the basics of programming. But if all you're trying to do is protect the program from your family this will work fine. If you're trying to make sure your program is actually secure, you'll need to encrypt the username and password and store them somewhere, and this is probably above your skill level at this point.
    • Marked as answer by Hamedeus Monday, August 18, 2008 7:29 PM
    Monday, August 18, 2008 7:14 PM

All replies

  • A double is a 64 bit floating point number, what you want to use are strings (string user, pass). Then you'll want something like:

    if (user != "hamadeus" || pass != "admin")
    {
        Console.WriteLine("You have entered the wrong username or password!");
        // exit program here
    }

    // continue program here

    But there are still many problems with your program. For one, it's not going to be very secure unless you're just trying to keep your little brother away from it. Anyone can use easy to find tools to look at your source code and get the username and password you've set in code because it's not encrypted. Second, you need to read up more on programming, readsome tutorials. When you define hamedeus and admin at the top, you are defining variables with those names, the strings don't actually have that value. The value is either "" (an empty string) or you might receive an error because you're trying to compare them before you give them a value. The code I have given you shows you how to compare the variables where the user input is stored with the actual strings "hamadeus" and "admin".

    So really you need to do some more reading on the basics of programming. But if all you're trying to do is protect the program from your family this will work fine. If you're trying to make sure your program is actually secure, you'll need to encrypt the username and password and store them somewhere, and this is probably above your skill level at this point.
    • Marked as answer by Hamedeus Monday, August 18, 2008 7:29 PM
    Monday, August 18, 2008 7:14 PM
  • Monday, August 18, 2008 7:22 PM
    Moderator
  • hey,

    thanks for the answer, and all the other info...

    i am very new to programming - do you know any good video tutorials for real "rookies"?

    thanks!
    Monday, August 18, 2008 7:30 PM
  • I am trying to makes sense out of you code. This is the first step. What I am trying to do here is to introduce some structure in it to the degree that when I paste it into my C# Editor it won't give me immediate errors. It is still not a compilation.

    You can see that I added an empty clock afte rthe if statement.

            private void Hamedeus ( )  
            {  
     
                double hamedeus, admin, user, pass;  
                Console.Write ( "\n\nUsername: " );  
                user = Double.Parse ( Console.ReadLine ( ) );  
                Console.Write ( "\n\nPassword: " );  
                pass = Double.Parse ( Console.ReadLine ( ) );  
                if ( user == hamedeus && pass == admin )  
                {  
     
                }  
                else  
                {  
                    Console.WriteLine ( "You have entered the wrong username or password!" );  
                }  
                Console.ReadLine ( );  
            } 
    OK, the next step is to make sure that you robjective is acomplishe dcorrectly. Although you can use Console.Write statements for announcements it is awfully inconvenient. You better use Console.WriteLine ( ).

    Now, I kind of played with your code and complicated it a bit. I don't understand why you are transforming your password to Double? How about your user name? Is Hamedous double? I doubt it.

    Anyhow. Here we go:

            private void Hamedeus ( )  
            {  
                double hamedeus, admin, user, pass;  
                hamedeus = Convert.ToDouble ("Hamedeus");    // this is bizarre, or course
                admin = Convert.ToDouble ("myPassword");    // this is bizarre, or course
                int counter = 0;  
                while ( true )  
                {  
                    counter++;  
                    if ( counter == 4 )  
                    {  
                        Console.WriteLine ( "That's it. Bye " );  
                    }  
                    if ( counter == 1 )  
                    {  
                        Console.WriteLine ( "\n\nPlease Type in Username: " );  
                    }  
                    else  
                    {  
                        Console.WriteLine ( "\n\nPlease Type in Username. You've got two more chances: " );  
                    }  
                    string input = Console.ReadLine ( );  
                    if ( input != string.Empty )  
                    {  
                        try  
                        {  
                            user = Double.Parse ( input );  
                        }  
                        catch  
                        {  
                            Console.WriteLine ( "Wrong characters, try again" );  
                            continue;  
                        }  
                    }  
                    else  
                    {  
                        Console.WriteLine ( "Empty String, try again" );  
                        continue;  
                    }  
                    Console.WriteLine ( "\n\nPlease Type in Password: " );  
                    pass = Double.Parse ( Console.ReadLine ( ) );  
                    if ( user == hamedeus && pass == admin )  
                    {  
                        Console.WriteLine ( "Thanks you. You can now go to sleep." );  
                        break;  
                    }  
                    else  
                    {  
                        Console.WriteLine ( "You have entered the wrong username or password!" );  
                    }  
                }  
            }  
     
    The code has not been tested, naturally.



    AlexB
    Monday, August 18, 2008 9:29 PM
  • Hamedeus;
      Welcome to the C# world!  You've picked one of the finest programming languages on the planet today.  I encourage you to search around the internet for any tutorials and examples you can find.  I went to google and typed in C# and found this http://www.google.com/search?hl=en&q=C%23

    Here's a bunch of links here: http://www.csharp-station.com/Links/ShowLinks.aspx?cat=CSharpSites&title=C%20Sharp%20Sites

    Petzold wrote some pretty good stuff on C#, but I like the books from APRESS best.  They do a very good job and are found here.

    Good luck and keep on coding....
    Javaman
    Monday, August 18, 2008 10:02 PM