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

Why is it when I write my code in workspaces, it's working perfectly, yet it wont work in the exam?

Why is it when I write my code in workspaces, it's working perfectly, yet when I add it in here it doesn't work and it counts the wrong number of Yay's. It says "I entered 5, but saw Yay! printed 6 times. "? I'm having trouble finding in the videos where this is even at, so I searched online. I've tried two different ways, each way giving me the same results. If I change the code even slightly at the while(n < i) line, it throws completely different random numbers.

Program.cs
using System;

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

            bool keepGoing = true; 


            while (keepGoing)
            {
                Console.Write("Enter the number of times to print \"Yay!\": ");
                string entry = Console.ReadLine();
               if (entry == null)
               {
                 return;
               }

              if (entry.ToLower() == "quit")
                {
                    keepGoing = false;
                }

            else 
            {

                    try
                {
                  int n = 0;
                  int i = int.Parse(entry);
                  while(n < i)
                    {                                       
                       {
                           Console.WriteLine("Yay!");
                           n++;

                         }



                   }
            }

                catch(FormatException)
              {
                Console.WriteLine("That is not valid input.");
                continue;
              }             
            }   
      }
   }                                     
}
}                                          

1 Answer

using System;

namespace Treehouse.CodeChallenges {
    class Program {
        static void Main() {
            bool keepGoing = true; 
            while (keepGoing) {
                Console.Write("Enter the number of times to print \"Yay!\": ");
                string entry = Console.ReadLine();
                if (entry == null) {
                    return;
                }

                if (entry.ToLower() == "quit") {
                    keepGoing = false;
                } else {
                    try {
                        int n = 0;
                        int i = int.Parse(entry);
                        while(n < i) {     
                            Console.WriteLine("Yay!");
                            n++;
                        }
                        keepGoing = false;
                    } catch(FormatException) {
                        Console.WriteLine("That is not valid input.");
                        continue;
                    }           
                }
            }  
        }   
    }
}

I added 'keepGoing = false;' after you printed 'yay' so the program would exit and not repeat. I would highly recommend watching your spacing, otherwise, it makes it extremely difficult to read your code.

Awesome! Thank you! That worked. I'll definitely work on my spacing as well.