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# Objects Loops and Final Touches For Loops

Hermes Crespo
Hermes Crespo
980 Points

This challenge does not make sense to me....

...because the I believe I did everything correctly but I am getting the error message: FrogStats.cs(13,30): error CS0019: Operator +' cannot be applied to operands of typeTreehouse.CodeChallenges.Frog' and `double'"... Well sure, that is because the type "Frog[] does not even exist! Arrays must be either of type: int, double, float or string not "Frog". How am I suppose to then use "+", "/" or any other normal math operator to get an average???

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        { 
            double total = 0;
            double AverageTongueLength = 0;

            for(int index = 0; index < frogs.Length; index++)
            {

                total = frogs[index] + total;

            }


            return AverageTongueLength = total / frogs.Length;
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

        public Frog(int tongueLength)
        {
            TongueLength = tongueLength;
        }
    }
}

1 Answer

Stephen Wall
PLUS
Stephen Wall
Courses Plus Student 27,294 Points

You seem to be getting that error in your for loop:

total = frogs[index] + total;

frogs[index] will return a Frog object, total is an int. So you are trying to add an int to a Frog which won’t work :)

You need to get the tounge length from each Frog in the frogs array. Try using some dot notation on: frogs[index].