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

Raymond Yau
Raymond Yau
811 Points

Compile error: unexpected symbol '{' and 'else'

Don't see any problems with the code. I've checked all the syntax and compared it to the code in the video.

https://w.trhou.se/64726bf5q2

whenever I compile I get 2 errors:

Program.cs(16,6): error CS1525: Unexpected symbol `{',

Program.cs(19,6): error CS1525: Unexpected symbol `else'

Any ideas guys?

Also, I find the Treehouse Workspace really hard to work with. I used to follow a book about C# and used Visual Studio throughout, I've never had to constantly check that my syntax, capitals, semi-colons as it would highlight the error instantly. I spend more time looking for syntax errors than actual programming!

1 Answer

andren
andren
28,558 Points

There are two issues in your code:

  1. You have not created a body for your while statement. In other words you have not created {} braces to hold the code that should be looped. Which means that the loop will consist entirely of the Console.Write command right under it which will loop infinitely.

  2. You have written if with a capital i which is invalid. That is what causes your compiler error.

If you add a body to your while statement that covers the code that should be looped, and change If to if like this:

using System;

class Program
{
    static void Main()
    {
        int runningTotal = 0;
        bool keepGoing = true;

        while (keepGoing)
        { // Added opening curly brace
            Console.Write("Enter how many minutes you exercised or type \"quit\" to exit: ");

            string entry = Console.ReadLine();

            if(entry == "quit") // Changed If to if
            {
                keepGoing = false;
            }
            else
            {
                int minutes = int.Parse(entry);
                runningTotal += minutes;

                Console.WriteLine("You've entered " + runningTotal + " minutes");
            }
        } // Added ending curly brace
        Console.WriteLine("Goodbye");
    }
}

Then your code will work.

Also as a side note I do agree that coding without any error checking is very tedious. In fact the way I found your errors was by opening your code in Rider (a C# IDE similar to Visual Studio) and simply looking at the errors it highlighted.

When I went though the C# courses on Treehouse I did not use the workspaces, but instead used an IDE. There is nothing stopping you from doing so as the programs you are writing are just standard Console applications which can be written and ran in any C# IDE.

As long as you watch the videos and complete the challenges it won't affect your course progress to not use the workspace.

Steven Parker
Steven Parker
231,210 Points

:warning: Be careful when using an external IDE with the challenges.

The IDE may be useful for detecting syntax errors, but will not check that your code meets the performance objectives of the challenges. I've seen many forum posts from folks who didn't take this into account.

Raymond Yau
Raymond Yau
811 Points

Thanks for your input andren. I never thought of pasting it into an external IDE. I'll probably use a combination of both the Workspace and Visual Studio.