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# C# Basics (Retired) Perform if / else

ali raafat
ali raafat
444 Points

i need help in my Program

so i am trying to make an averager. I made a loop, at the numbers entered by the user but i need to find the user average when he types done. how can i make it . I tried to run it without finding the average but when i typed done the Program crashed.

Feisty Mullah
Feisty Mullah
25,849 Points

Hi ali, If you could show us your codes it would be easier to understand the issue. Just paste your codes here and we will try to help .

Steven Parker
Steven Parker
229,732 Points

Yes, please remember to show your code. If you are using the workspaces, you you can use the snapshot function in the workspace and provide the link to that.

1 Answer

Henrique Vignon
PLUS
Henrique Vignon
Courses Plus Student 6,415 Points

In order to find an average of anything you just have to divide the total sum of numbers by the number of numbers

So it would be something like this:

            string entry;
            double sum = 0.0;
            int count = 0;

            while (true)
            {
                Console.Write("Enter numbers to find their average, type \"done\" to finish: ");

                entry = Console.ReadLine();

                if (entry.ToLower() == "done")
                {
                    if (count > 0)
                    {
                        Console.WriteLine("The average is: " + sum / count);
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Nothing to average");
                        break;
                    }

                }

                sum += double.Parse(entry);
                count++;
            }

You'd have to add some checking to guarantee the person types valid numbers and whatnot, also you can add in some flair by changing the input message so like "Now type the second number", then "third number" and so forth.

But you can figure how to do that by yourself :)