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

Maria Kochetygova
PLUS
Maria Kochetygova
Courses Plus Student 1,784 Points

I am confused, and don't know how to fix it

What is wrong with my code?

Program.cs
using System;

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

          int count;
          int i=0;
          Console.Write("Enter the number of times to print \"Yay!\": ");
          count=Console.ReadLine();
          while(i<count)
          {
          try{
            int number =int.Parse(count);
          }
          catch (FormatException){
          Console.WriteLine("You must enter a whole number.");
          continue;
          }


          Console.WriteLine("Yay!");
         i++;
          }
        }
    }
}

If you don't mind selecting the answer so other's can know this has been answered successfully.

4 Answers

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        { 
          var i=0;
          Console.Write("Enter the number of times to print \"Yay!\": ");                    
          try
         {
            var count=Int32.Parse(Console.ReadLine());
            while(i<count)
            {
              Console.WriteLine("Yay!");
               i++;
            }
          }
          catch (FormatException)
         {
          Console.WriteLine("You must enter a whole number.");
          }  
        }
    }
}

Just cleaned it up a bit and removed the extra variable. So you're setting count as an int, setting i = 0. Then you prompt the user for the number of times to print Yay. You then enter a Try/Catch. Inside your Try you put the assignment of the input to count which could actually be var count=Int32.Parse(Console.ReadLine()); if there isn't an exception you'll go into the while loop printing out Yay! the correct number of times. If there is an exception you'll write out the correct message for that.

Cheers....

For easier reading

Hi Eric,

I think that your use of var count is causing an error.

Program.cs(10,11): error CS0818: An implicitly typed local variable declarator must include an initializer
Program.cs(15,13): error CS0841: A local variable `count' cannot be used before it is declared
Program.cs(16,21): error CS0841: A local variable `count' cannot be used before it is declared
Compilation failed: 3 error(s), 0 warnings

I believe that you need to either initialise it to 0 so that the compiler knows that it is an int or just declare it using int

var count = 0;
int count;
Seth Kroger
Seth Kroger
56,413 Points

Remember that Console.ReadLine gives you a string, not an int. You have to take that string and convert it to an int for it to work.

Seth Kroger
Seth Kroger
56,413 Points
string response = Console.ReadLine();
count = int.Parse(response);
Seth Kroger
Seth Kroger
56,413 Points

You also need to take the try/catch block outside of the while loop and have the above inside the try block when you get to that part of the challenge. The only the loop should do is write "Yay!" and increment the loop counter.

Thanks, bothxp. Sorry, I just ran it and came back to edit. Guessed I missed that. Should be good now.