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 trialCameron Nazarko
810 PointsI don't know how to get the average of my numbers.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace Averager { class Program { static void Main(string[] args) {
double averageofnumbers = 0.0;
while (true)
{
Console.Write("Enter a number or type \"done\" to calculate average of numbers : ");
var entry = Console.ReadLine();
try
{
if (entry.ToLower() == "done")
{
Console.WriteLine("average of numbers = " + averageofnumbers);
continue;
}
var numbers = double.Parse(entry);
}
catch(FormatException)
{
Console.WriteLine("That is not valid input");
}
if (entry.ToLower() == "done")
{
Console.WriteLine("average of numbers = " + averageofnumbers);
continue;
}
}
}
}
}
//I'm not sure how to get the average for my numbers. I've got the user input pretty squared off as well as the Format Exception. Anyone know a solution??
3 Answers
Lukas Dahlberg
53,736 PointsWhere in your code are you actually calculating the average? You'll need to store the entries and then do your mathematics on that collection.
Also, you've got the "done" code duplicated.
Lukas Dahlberg
53,736 PointsCan you please link to the challenge you're trying to solve?
Lukas Dahlberg
53,736 PointsOK, so the key is to break down the problem a bit more.
1st, we know that we need a running total. There are two ways we can accomplish this:
First, we can create a list and add each entry to it. Then, after breaking the for loop, we can loop over the entries, add them all to a total and then divide by the list.
The alternate way is to create the sum by adding the entry to it directly, and then keeping a separate variable that tracks how many iterations there have been. Then at the end we can divide the sum by the number of iterations.
A couple problems you're going to run into with your current implementation:
'Continue' does not break the while loop. You need to use the break keyword. Continue just starts over (leading to an infinite loop).
Second, your second done if statement should be deleted.
I hesitate to provide more information than the above as it would essentially mean solving the practice project for you. This is a good time to use the above information to help solidify your understanding of C#.
Cameron Nazarko
810 PointsCameron Nazarko
810 PointsI'm trying to calculate the average when the user inputs "done". then it will add together the running total from the previous inputs and divide the number of items by the total number. However, I'm not sure how to accomplish this.