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

Eugene Lee
Eugene Lee
11,890 Points

i have been tying solve C# basic final q1 for many days, but i failed. i am really obnubilated big time. can u help

i did for loop, while loop, if else, but i get either parsing error or yay thing prints once. i googled up & down only to redound me to get obfuscated. i even seen some some people posted solutions that are nothing but dissembling pitfall with impletion with parsing errors. i have been getting certificates from other online schools so far. but, this one is clueless. do you have upfront advice. thanks

Program.cs
using System;

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

            try {
            int count = int.Parse(input);

            int i = 0;
            for (int i = 0; i < count; i++)
            {  
                Console.WriteLine("Yay!");
            } } catch(FormatException) {
                Console.WriteLine("You must enter a whole number.");
            }
        }
    }
}

2 Answers

Steven Parker
Steven Parker
229,921 Points

The loop in the provided code was fully functional and did not need to be changed. As a side effect of the changes, the variable "i" is now declared twice, but variables can only be declared once:

            int i = 0;                       // i is declared as int
            for (int i = 0; i < count; i++)  // i is declared as int (again)

So you could put the loop back the way it was, or remove one of the two declarations of "i".

Hopefully that will get you out from behind that cloud!

Eugene Lee
Eugene Lee
11,890 Points

thank you so much. i will give it a shot. thanks again