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#

Averager Project C#

Hi everyone, I would really appreciate it if you guys can give me a feedback about my project C# Averager.

Snapshot : Project SnapShot

1 Answer

Allan Clark
Allan Clark
10,810 Points

Forewarning, I have not run this code locally just eyeballing it.

First thing that pops out at me is the error message "No numbers have been filled, please try again". This check is only hit if the user inputs 'done'. Being that you are accepting comma separated values for the average it would seem the user would only need to input 'done' to exit the program (at which point they won't care if the list is empty). You can either stick with the CSV format and remove that check entirely, or you could restructure the program to have the user input the values one at a time.

Secondly, line 35 is doing a lot all at once. I would suggest separating this out into multiple lines for readability sake also you can improve on error handling if you do. We have to think about all possible user errors, suppose user were to input a letter somewhere in the values. For example, 1,2,3,e,5,6. This would likely cause an exception and crash the program. I would suggest the code to flow something like the following pseudo code:

var stringArray = answer.Split(new[]{','},StringSplitOptions.RemoveEmptyEntries);

foreach(var input in stringArray){
       //try parse to int
       //if not int display message and exit loop
       //add new int to numbers list
}

The rest looks pretty good.

Also as a side thought, restructuring to get the digits individually from the user (then 'done' after their last input) you would be able to try to parse the input and prompt in case of an error immediately rather than after all the user input is done. If we went back to the 1,2,3,e,5,6 example, the user would have to input all the values again, where as if you were taking them individually they would just reinput the one they errored on.

All in all a great start! keep up the good work and happy coding!