Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Basics (Retired) Perfect Final

Lars Segelke
Lars Segelke
3,049 Points

Whats wrong?

I get this error, but I don´t know what is wrong.

Program.cs(11,23): error CS1525: Unexpected symbol i', expecting.' Program.cs(11,44): error CS1525: Unexpected symbol )', expecting;' or `}' Compilation failed: 2 error(s), 0 warnings

It says The "i" in the ReadLine is wrong, but that is not possible. Also it says The ")" is wrong, but a ";" or a "}" doesn´t make sense :(

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");

            var times = Console.ReadLine();

            while (int i = 0; i < times; i++)
            {
                Console.WriteLine("Yay!");   
            }
        }
    }
}

1 Answer

James Churchill
STAFF
James Churchill
Treehouse Teacher

Lars,

Looking at loop syntax, it looks like you're trying to use a for loop, but you're using the while keyword instead of the for keyword. Replacingwhilewithforwill resolve your current error. But then you'll get a new compiler error because you're trying to compare the return value from theReadLine` method, which is a string, to an integer.

If you wanted to use a while loop, you could rewrite your code like this:

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");

            var times = int.Parse(Console.ReadLine());
            int i = 0;

            while (i < times)
            {
                Console.WriteLine("Yay!");
                i++;
            }
        }
    }
}

Thanks ~James