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 Wrap Up

Tony Lawrence
Tony Lawrence
3,056 Points

Trouble on the Final Challenge

Hi again.

Having trouble figuring out with figuring out the trouble with the Final Challenge.

So far I've made this:

using System;

namespace Treehouse.CodeChallenges { class Program { static void Main() {

        while (true) {

            Console.Write("Enter the number of times to print \"Yay!\": ");

            string input = Console.ReadLine();

            var redo = int.Parse(input);

                    if (redo != 0) 
                        {
                                   Console.WriteLine("Yay!");
                                     redo -= 1;
                    }

                 }
        }
}

}

Though I keep getting errors such as unexpected } on the lower part, even though I know all the brackets are set up. Then getting errors stating that String is giving out an error with int.Parse or Int.Parse. Trying to go with this answer with a var approach. Not sure what exactly is the problem to determine if I'm doing it right or wrong.

1 Answer

andren
andren
28,558 Points

The problem is your while loop, and there are actually two problems, firstly since the while loop wraps around all of your code that means everything is run multiple times, including the Console.ReadLine method which will ask for new input each time, the treehouse challenge is only setup to provide input once so that is why you are getting errors about the string not being set properly.

Secondly your loop never ends, whenever you start a while loop with true as the condition you have to include a break statement to stop it, which you have not done.

If you remove the while statement and then change your if statement to be a while statement instead (using the same condition as the if statement currently uses) then your code will run fine.