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

ali raafat
ali raafat
444 Points

can any one correct me

plz can anyone tell me my mistake but plz i just need to know how to fix it not the whole answer.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
                 Console.Write("Enter the number of times to print \"Yay!\": ");
            var entry = Console.ReadLine();
            if(entry == "Yay!")
            {
                Console.WriteLine("Yay!");

            }


        }
    }
}

3 Answers

Steven Parker
Steven Parker
231,108 Points

You have a bit of work to do yet, here's a few hints:

  • what the user types will be a number, not a word
  • what comes in from ReadLine is a string
  • you will then need to convert that string into a number
  • you will need a loop to write the output several times
  • the loop will use the converted number to control how many times to write the output

I'll bet you can take it from here.

ali raafat
ali raafat
444 Points

how can i use the loop to convert number to control how many times to write the output

Steven Parker
Steven Parker
231,108 Points

For example, let's say you convert the string to a number in a variable named times:

var times = int.Parse(entry);

Then, you could create a counter variable and a loop that compares it to that number:

var loop = 0;
while (loop < times) {
    /* print stuff here */
    loop += 1;
}

You might want to review Looping.

ali raafat
ali raafat
444 Points

can you explain to me what does counter variable mean and can you explain to me this part : ( /* print stuff here */ loop += 1;), one last thing why did you add a loop variable.

Steven Parker
Steven Parker
231,108 Points

"Counter variable" is just any variable used to keep a count, in this case it is the number of times the loop code has run.

In the example above, loop is the name of the counter variable.

The comment "/* print stuff here */" is just a place holder to remind you that this is where you would put the code to print the phrase that the challenge wants to see.

The statement "loop += 1;" increments the counter variable to show the loop code ran.

ali raafat
ali raafat
444 Points

so loop += 1; is used to print what user entries in the variable loop yes? why did you use this (loop < times), sorry but Iam confused what should i do after that and what should i write in "/* print stuff here */" . sorry i know you helped me a lot a i really appreciated it. how can i contact you ?

Steven Parker
Steven Parker
231,108 Points

No, "loop += 1;" increments the count. It does not print.

The "while (loop < times)" controls the loop. It keeps it going until it runs the correct number of times.

The comment is where you could put this line (which does print):

                Console.WriteLine("Yay!");

I'm just one of the students here, like yourself. When you post here any other student can answer you.

I am glad I could help. Happy coding!

ali raafat
ali raafat
444 Points

i wrote the same thing but got a compiler errors. ( while and } ) are unexpected symbols ?? how?

Steven Parker
Steven Parker
231,108 Points

That error is not related to the WriteLine statement. You'd need to show what the whole thing looks like now.

If you still need help, start a new question.