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

C# Basic final part 2

So when I ran this in Visual Studio the count variable is not working. The console ask for a value and if a number larger than 0 is entered, "Yay!" is printed but only once. The program is not running through the loop until the count variable and user input are the same. Thanks for the help in advance.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            var count = 0; //holds yay amount for each time the loop is performed

            while (true)
            {
                Console.Write("Enter the times to type \"Yay!\" : "); //ask for yay amount

                var input = Console.ReadLine(); //assigns the users input to a variable called input

                try //passing the value through the loop
                {
                    var inputTimes = int.Parse(input); //because input is a string, needs to be converted to an integer

                    if (inputTimes <= count) //if int value for times user entered is less than the count value
                    {
                        Console.WriteLine("That is not enough for A Yay!"); //message to the user that no yay's will be printed
                        continue; //jumps out of loop and restarts the program
                    }

                    else //if input value is larger than count value
                    {
                        Console.Write("Yay!"); //print "Yay!" once
                        count++; //count value should have 1 added to it, loop goes through again. if user input is larger than count, "Yay!" is printed and 1 is added to count untill count and input are equal
                    }
                }

                catch (FormatException) //catch a format exception
                {
                    Console.WriteLine("That is invalid input. Enter a whole number.");
                    continue;
                }
            }
        }
    }
}

1 Answer

Jon Wood
Jon Wood
9,884 Points

It's printing only once when input is greater than 0 because of these lines: You initialize count to zero here.

var count = 0;

But in your check here:

if (inputTimes <= count)
{
  Console.WriteLine("That is not enough for A Yay!"); 
  continue;
}
else
{
  Console.Write("Yay!");
  count++; 
}

It's always going to go into the "else" statement and in there you only print "Yay!" once. So you just need to print it as many times as the user input.

Also, from what I can tell, it doesn't look like your loop ends. You may need to add a break after you print to the console.