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

Furkan Demirkan
Furkan Demirkan
7,926 Points

I'm stumped on what's wrong with my code. Anyone else getting the same issue?

I'm pretty sure it should work...

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            int num = Console.Read();
            int i = 0;
            while(i < num){

            Console.WriteLine("Yay!");
            i+=1;
            }
        }
    }
}
Furkan Demirkan
Furkan Demirkan
7,926 Points

also, I have tried using a for loop:

for(int i = 0; i <num; i++) { Console.Write("Yay!"); }

It is possible that I am mixing up some Java without realizing it, but I can't see anywhere I might have gone wrong.

1 Answer

Steven Parker
Steven Parker
231,269 Points

:point_right: You may not be interpreting the input correctly.

Your loop itself seems good, but the Console.Read function returns a numeric code representing a single input character. That might not be the intended number of times to repeat.

You might have better results using Console.ReadLine to bring in a complete input line as a string, and then doing something to convert that string into a number to control the loop.

Damien Watson
Damien Watson
27,419 Points

I've only touched C# and haven't done any of the courses, but a quick google search will show you the way. Tempted to paste my code solution in here, but Steven's comments will get you on track. (hint: use 'var' not 'int' for ReadLine;).

Furkan Demirkan
Furkan Demirkan
7,926 Points

True, I forgot to parse it to an int. Thank you.