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 trialLi Yu
2,067 PointsC# error CS1525: Unexpected symbol `}'
I am sure that every "{" has its equally paired "}" in my code, but the compiler keep telling me that Program.cs(20,40): error CS1525: Unexpected symbol `}'. I wonder where my mistake is, and how can I fix it?
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
while(true) //loop started
{
Console.Write("Enter the number of times to print \"Yay!\": ");
string entry = Console.ReadLine();
if(entry.ToLower()=="quit")
{
break;
}
else
{
int time = int.Parse(entry); //turn "string" into "int"
int k;
for(k = 0; k < time) //make the loop run k times
{
Console.Write("Yay!");
k = k + 1;
continue;
}
}
break; //leave the "while loop" since "for" is not satisfied
}
}
}
}
2 Answers
Steven Parker
231,248 PointsYour program "works", but deviates from the instructions.
This is a great example of how something that works (will compile an run) might not be what was asked for (satisfies the requirements). This has been a topic of many previous questions, but I'll repost the info here:
People often over-think this one and get too creative, and their answer is rejected for a variety of reasons (some which don't seem to make any sense).
Here are the secret "rules" you must follow for success on this one:
- Your program must ask for and accept input ONE TIME ONLY
- If the input validates, your program will perform exactly as in Task 1
- If the input does not validate, it will print the error message and exit
- Validation must be done by exception catching. There are other perfectly good methods that can be used for validation, but they will not pass this challenge
It looks like you have an extra loop that is violating the first and third "rule".
Avan Sharma
7,652 Pointsusing System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
Console.Write("Enter the number of times to print \"Yay!\": ");
int entry = int.Parse(Console.ReadLine());
for(int i=0;i<entry;i++)
{
Console.WriteLine("Yay!");
}
}
}
}
Li Yu
2,067 PointsLi Yu
2,067 Pointswhile(true) //loop started { Console.Write("Enter the number of times to print \"Yay!\": "); string entry = Console.ReadLine(); // accept one input only {