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#

James Hammond
James Hammond
1,297 Points

Verifying user input to items in a list

I am working on my coffee shop console app for practice, and ran into a bit of a scenario.

My code asks the user if they want something on the Menu. It then prints the list out, and the user types in what they want. If they want another item it prompts the user, and then they put in whatever other item they want. The issue is, they can type Alphagetti (which is not on the list) and it will add it to the list.

How do I check the list to the user's answer and verify if what they typed in is an actual item on the menu?

1 Answer

Steven Parker
Steven Parker
231,261 Points

A couple of ideas come to mind. One, you could provide a checklist of (only) available items and the user could make their selections by checking boxes instead of filling in text.

Another would be to loop through a list of the available item titles and compare the input to each item for a match. If none matched you could require the user to input again.

James Hammond
James Hammond
1,297 Points

Great idea, i forgot to mention it was a console application , so I wonder if the second suggestion may be my best option :P