Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C#

Amber Cook
Amber Cook
4,739 Points

How to allow user to try & start again if the username and/or password doesn't match. - For a BASIC C# login console app

The app works if you enter all the details correctly and even if you don't enter the details to match the requirements, the program will still continue onto the next part. (Requirements being both username and password have to be at least 8 characters, and passwords have to be entered twice & match.) I am unsure how to code it so they can start again if their entry does NOT match the requirements. The app has a main method to collect users input for both username and two password entries storing them in variables. The main method also calls 2 methods. 1 for checking username length and returning whether or not the username is valid. And the second method is for checking the length of the password entries and whether or not the passwords match. I don't kknow how to use the if else statements to check for one thing before allowing the user to continue onto the next. This is for an assignment and I am only supposed to use the main method and 2 other methods for checking so I don't want to go overboard with making more methods. Thanks!

Amber Cook
Amber Cook
4,739 Points

I guess what I am trying to say is, "How can I repeat the question until they give a valid input?".

1 Answer

Allan Clark
Allan Clark
10,810 Points

The easiest way would be to use a do-while loop and break out of it when the credentials are correct. The flow should be something like the following pseudo code:

do {

     //prompt user for creds
     bool credsMatch = //check user creds

} while(!credsMatch)

Let me know if i can assist further. Happy Coding!

Amber Cook
Amber Cook
4,739 Points

Thank you for your help! Works perfectly!