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

Stuck here, please help. Thanks

I've put it in visual studio and changed it to not get compile errors but still not the desired result.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            var totalYay = 0;

            while (true)
            {
                //var entry = 0;
                Console.Write("Enter the number of times to print \"Yay!\": ");
                var entry = Console.Read();
                try
                {
                    var timesYay = double.Parse(entry);

                    if (timesYay < 0)
                        {
                            Console.WriteLine(timesYay + "is not a valid input, please try again!");
                            continue;
                        }
                    else if (totalYay > timesYay)
                        {
                            break;
                        }

                    else
                        {
                            Console.Write("Yay");
                            totalYay = totalYay + 1;
                        }

                    }
                    catch(FormatException)
                    {
                    Console.WriteLine("Please try again");
                    }
            }
        }
    }
}

2 Answers

Scott Wyngarden
PLUS
Scott Wyngarden
Courses Plus Student 16,700 Points

The challenge is asking you to do two (or maybe three, depending on how you like to think about it) things, so think about structuring your code so that it's obvious what thing you're doing at a time. Trying to put it all in the single while loop can make it really complicated to try and track all the things the two pretty different operations can do.

Put another way, I'd think about tackling this by using two logical groupings.

  1. one to make sure I got a valid input (an int, probably)
  2. and another one to print out the text the amount of times the user requested.

as some pseudocode, this should get you a lot of the way there for what to add to the code you were given to start with:

string userEntry = "";
int yayTimes;

//Group 1: Get & Validate information
  // Get the user input
  // check that it's can be parsed or cast to the desired type;
  // assign it to yayTimes;
  // check that it has the right value range;

//Group 2: Print out the text
for(int i = 0; i < yayTimes; i++) {
 //print the text
}

Thank you very much, worked it out!