Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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!