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) Perfect Wrap Up

Average program challenge problem, Im getting an error and i dont know why

This is the code:

using System;

namespace TreeHouse.Averager
{
    class Averager
    {
        static void Main()
        {
            int totalAverage = 0;
            int finalAverage = 0;
            int count = 0;


            while(true) {
               Console.Write("Enter a number or type 'done' to exit the program: ")
               string entry = Console.ReadLine();

               if(entry.ToLower() != "done") {
                   continue;   
                }
                else {
                     Console.WriteLine("The average is : {0}", finalAverage);  
                     break;   
                }
               try
                {
                  int calculateAverage = int.Parse(entry);
                    totalAverage += calculateAverage;
                    count += 1;
                    finalAverage = totalAverage / count;
                }

                catch(FormatException)
                {
                    Console.WriteLine("Invalid input, Try again!..");
                    continue;      
                }  

            }



        }
    }
}

I'm getting this error when i try to run the program :

Averager.cs(16,15): error CS1525: Unexpected symbol `string'     

I dont know why i get this error, the code looks correct, I will be happy for some help.. (i dont use double or var yet because i want to try it first this way)

4 Answers

Steven Parker
Steven Parker
231,248 Points

I think we're going off the original topic here, but I'll answer your questions.

First, good job on the program! I have only a few minor and mostly cosmetic suggestions:

  • The "calculateAverage" isn't needed, you could write: "totalAverage += double.Parse(entry);"
  • give each level of nesting a different amount of indentation
  • space each indentation level consistently throughout the program
  • use blank lines sparingly for easier reading

I do still consider C# a good language, and it is the primary professional skill of my current position. In terms of pure popularity, JavaScript and Java both beat it, but popularity and salary ranking is often different. But I should point out that I also use JavaScript quite a lot in full-stack development. But in any case, I'd suggest researching recent statistics (and projections!) carefully before making a career decision.

Xamarin would be a good skill if you have an interest in mobile programming. It's not likely to be needed for web development.

For now, I believe .net is still much more widely used than .net core. I have suggested starting new projects using .net core at work and have always been told "we're sticking with MVC .net for now".

I hope that covers everything. Happy coding!

Steven Parker Brendan Whiting

any chance for help?

Steven Parker
Steven Parker
231,248 Points

:bell: Hi, I got alerted by your tag, but it looks like Brandon beat me to it!

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 Points

You're missing a semicolon at the end of line 15. That's why there's an "Unexpected symbol `string'" on line 16.

The error message shows the line and character number in the file: Averager.cs(16,15): It's pretty common for errors to be caused by something in the code right before it, keep that in mind when troubleshooting.

Steven Parker Hi!, any thoughts or best practices on my final code?:

using System;

namespace TreeHouse.Averager
{
    class Averager
    {
        static void Main()
        {
            var totalAverage = 0.0;
            var finalAverage = 0.0;
            var count = 0;



            while(true) {
                   Console.Write("Enter a number or type 'done' to exit the program: ");
                   var entry = Console.ReadLine();

                   try {
                       if(entry.ToLower() != "done") {

                           var calculateAverage = double.Parse(entry);
                            totalAverage += calculateAverage;
                            count += 1;
                            finalAverage = totalAverage / count;



                        }
                        else {
                             Console.WriteLine("The average is : {0}", finalAverage);  
                             break;   
                        }

                     }

                catch(FormatException) {
                    Console.WriteLine("Invalid input,Try Again!..");
                    continue;     
            }


           }
    }
}

}

Thanks!!

btw steven, do u consider c# a good language to learn nowdays? i can do websites, mobile apps and desktop application with it, i know i can do this with java or js as well what your thoughts?

i have few other questions: 1.another question i have is this, after i finish c# begging and c# intermediate i should continue to xamarin? >> what is the proper roadmap in ur opnion?

  1. I should start to learn asp.net or skip it and learn .net core instead?, treehouse dont provide any courses on .net core, only a workshop.. , and i heard that .net core is very good.

thanks in advance!