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

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!