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,343 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.