Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Basics (Retired) Perfect Final

Alex Lee
Alex Lee
408 Points

C+ Basic final task STUCK

Hi I could solve this task at all, if someone could help me and points out what I did wrong it would really be appreciated, if possible please provide a solution. Thanks so much.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");

            try {
            string input = Console.ReadLine();
            int total = int.Parse(input);
            int count =0;

            if (total<0)
            {
                Console.WriteLine("You must enter a positive number.");
                return;
            }


            while (total>count)
                Console.WriteLine("\"Yay!\""); 
                counter++;
                }

            catch (FormatException) {
                Console.Write("You must enter a whole number.");
            }

        }
    }
}

4 Answers

It may be may be the counter++ in your while loop. Should it not be count++ ?

using System;

namespace Treehouse.CodeChallenges { class Program { static void Main() {
try { string input = Console.ReadLine(); int total = int.Parse(input); int count =0;

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

        if (total<0)
        {
            Console.WriteLine("You must enter a positive number.");
            return;
        }


        while (total>count){
            Console.WriteLine("\"Yay!\""); 
            count++;
            }

        }

        catch (FormatException) {
            Console.Write("You must enter a whole number.");
        }          
    }
}

}

This works. The catch should not be inside to try. The while loop was missing a curly brace after the parentheses. When asking for a number, you had console.write but you must have console.writeline. (sorry about the solution post it didn't come out right).

Sorry to say but there is no need for the total. As the count already did what we needed. and as this just need to be compared so we don't get a negative number we just need to make sure the the number the user inputs is higher than 0. So it easier just to compare the count against the 0. and if the count is less than 0 you should write out the Console.WriteLine();

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            try
            {
                Console.Write("Enter the number of times to print \"Yay!\": ");
                string input = Console.ReadLine();

                int count = int.Parse(input);
                if(count>0)
                {
                    int i = 0;
                    while(i < count)
                    {
                        i += 1;   
                        Console.WriteLine("Yay!");
                    }
                }
                else
                {
                    Console.WriteLine("You must enter a positive number.");   
                }
            }
            catch(FormatException)
            {
                Console.WriteLine("You must enter a whole number.");
            }

        }
    }
}