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

Michael Thompson
Michael Thompson
7,427 Points

Don't know where to go from here. Final challenge problem.

I'm struggling trying to figure out how to get it to print "Yay!" the amount of times the user inputs.

Program.cs
using System;

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



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

          var a = Console.ReadLine();

          try 
          {
            var b = double.Parse(a);

            for(b=1; b<10; b++)
            {
              if (b > 0)
              {
              Console.Write("Yay!");
              }

              else {
                Console.Write("That is not a valid input."); continue;
              }

            }
          }
          catch(FormatException)
          {
            Console.Write("That is not a valid format."); 
          }

        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,248 Points

The "for" loop replaces what you converted from the console input and always repeats 9 times.

To repeat the correct number of times, you can create a new loop variable to compare with "b", or you could skip the initialization and decrement "b" until it reaches 0.

Steven Parker
Steven Parker
231,248 Points

I've commented the lines involved:

            var b = double.Parse(a);
            // this converts the reply into a number and stores it in "b"

            for(b=1; b<10; b++)
            // this DISCARDS the contents of b, then uses it to count from 1 to 9

My suggestions would involve replacing the loop line. To create a new loop variable to compare with "b":

            for (int n=0; n < b; n++)

or you could skip the initialization and decrement "b" until it reaches 0:

            for (/* no initialization */; b > 0; b--)

But I have to wonder ... wasn't this part of the code working when you passed task 1?

Steven Parker
Steven Parker
231,248 Points

Task 1 is the basic loop. I assumed you had passed that because it's not until task 2 that the instructions ask you to detect and respond to parse errors.

If you're still on task 1, none of that code should be there.

Michael Thompson
Michael Thompson
7,427 Points

I wrote more than I needed for the first task. Didn't fail it but wasn't necessary till part 2. I got to task 3 and now I'm hung up on it. Thanks for your help.