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

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
218,679 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