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 Final

Xuanzheng Lin
Xuanzheng Lin
2,466 Points

Unexpected symbol `int'

First question: based on the console "Program.cs(13,16): error CS1525: Unexpected symbol `int' Compilation failed: 1 error(s), 0 warnings" which line breaks?

Second question: how do I fix my code?

Thanks : )

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            int counter = 0;
            var input = Console.ReadLine();
            int number;
            try {
                Int32.TryParse(times, out number)
                int times = Int32.Parse(input);
                while (counter < times) 
                {
                    Console.WriteLine("\"Yay!\"");
                    counter ++;
                }
            } catch (FormatException) {
                Console.WriteLine("You must enter a whole number.");
            }
        }
    }
}

2 Answers

Raffael Dettling
Raffael Dettling
32,999 Points

Delete int number and Int32.TryParse(times, out number) to pass stage 1 and 2. You can declare an empty int variable but you dont need this 2 lines of code they throw the error. One reason is that you decleare the variable "times" after you using it and you already converted the string with Int32.Parse(input) to an int type :) Do you need help at task 3?

Xuanzheng Lin
Xuanzheng Lin
2,466 Points

Hey,

I don't think Int32.TryParse(times, out number) is working. Also, could you explain why you wrote out number but never state what number is?

Patrik Horvรกth
Patrik Horvรกth
11,110 Points

in my opinion

1st CATCH is wrong you have to add ref on your exception to CALL error patch or description - catch (FormatException e)

2nd you cant use empty value "number"; for example give him value 0 at the start

3rd static void Main(string[] args) its RULE