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

Muhammad Bah
Muhammad Bah
852 Points

I cannot find a way to print as many "yays" as the user inputs.

I am trying to print to the console as many "yays" as the user inputs. However, I am stuck at the while loop. I do not know how to tell the computer to print as many "yays" as the user desires. Please help me solve this problem.

My code goes as follows (inside the Main method):

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

        string numberGiven = Console.ReadLine();

        int convert = int.Parse(numberGiven);

        while (convert == convert)
        {
          Console.WriteLine("Yay!");
        }
Remco de Wilde
Remco de Wilde
9,864 Points

Your trying to test convert to convert this is always true.

better is to us a for loop.

Regards Remco

2 Answers

andren
andren
28,558 Points

The problem is the condition of your while loop, you are stating that it should run as long as the number entered equals the number entered, which it will always do since those are the same thing, leading to an infinite loop.

If you want to complete this challenge using a while loop (there is a loop type called a for loop that is better suited for this type of task but I'm pretty sure the course has not covered that yet) then you have to manually create an count variable outside the loop which will keep count of the number of times the loop has run. That way you can use the count as part of the condition for when to stop the loop. Like this:

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

string numberGiven = Console.ReadLine();
int convert = int.Parse(numberGiven);
int count = 0;
while (count < convert) // Run while the count is less than the number entered
    {
        Console.WriteLine("Yay!");
        count++; // Increase the count by 1.
    }
Muhammad Bah
Muhammad Bah
852 Points

I tried your method and it somewhat worked. The only issue is that the 'yay' is only displayed once and then I have to press a key every other time until it reaches the number I chose at the beginning.

Muhammad Bah
Muhammad Bah
852 Points

Actually, it got accepted into the quiz! Thank you very much! I apologize for thinking it didn't work at first.

Jon Wood
Jon Wood
9,884 Points

I can't recall if you get into for loops yet or not, but that seems best to use here.

You already convert the input, which is great! Use this for loop skeleton to try to solve it. If you need any more help with it just let me know.

for(int i = 0; i <= convert; i++)
{

}
Muhammad Bah
Muhammad Bah
852 Points

The part of the course I am at right now does not allow the use of 'for' loops.