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

Muhammadreal Rizwanstuff
Muhammadreal Rizwanstuff
915 Points

Cannot make it work the way it was asked

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
    int counter = 0;
       Console.Write("Enter the number of times to print \"Yay!\": ");
       string yaynumber = Console.ReadLine();
       string yay = "Yay!";

        counter = int.Parse(yaynumber);
       for (int i = 0; i <= counter; i++)
       {
           Console.WriteLine(yay[i].ToString());
       }

       Console.ReadLine();
        }
    }
}

This is not printing word yay . Can any help me what i have doing wrong?

3 Answers

Steven Parker
Steven Parker
231,248 Points

You got very close, but you did something odd with your string by applying an index ([i]) that would pick out a single character at a time. It would also cause errors when printing more than 4 lines, since you run out characters in your word. Plus, you also have a stray extra ReadLine after the loop.

See the comments in the corrected code below:

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            int counter = 0;
            Console.Write("Enter the number of times to print \"Yay!\": ");
            string yaynumber = Console.ReadLine();
            string yay = "Yay!";

            counter = int.Parse(yaynumber);
            for (int i = 0; i <= counter; i++)
            {
//              Console.WriteLine(yay[i].ToString()); <-- this prints just one letter of the word
                Console.WriteLine(yay);;               // this prints the whole word
            }

//          Console.ReadLine();                       <-- this waits for input that is not used
        }
    }
}
Muhammadreal Rizwanstuff
Muhammadreal Rizwanstuff
915 Points

Thanks Steven,

using System;

namespace Treehouse.CodeChallenges { class Program { static void Main() { int counter = 1; Console.Write("Enter the number of times to print \"Yay!\": "); string yaynumber = Console.ReadLine(); var yay = "Yay!";

    var counter1 = int.Parse(yaynumber);
   for (int i = 0; i <= counter; i++)
   {
       Console.WriteLine(yay);
       if (counter1 <= counter)
       {
           break;
       }
       else if (counter1 <= 1)
       {
           Console.WriteLine("please enter valid number");
       }
       counter += 1;

       continue;


   }


    }
}

}

This code work perfect if i want to print yay. But it will still print if i enter 0 or -numbers. And it will not continue.

Steven Parker
Steven Parker
231,248 Points

Remember to choose a "best answer". :)

And I was a bit confused by your last comment. Did you mean for that to be a new question?

Muhammadreal Rizwanstuff
Muhammadreal Rizwanstuff
915 Points

I tried to toString() already but it did not work.

this code worked but as i typed 0 its still printing one.

using System;

namespace Treehouse.CodeChallenges { class Program { static void Main() { int counter = 1; Console.Write("Enter the number of times to print \"Yay!\": "); string yaynumber = Console.ReadLine(); var yay = "Yay!";

    var counter1 = int.Parse(yaynumber);
   for (int i = 0; i <= counter; i++)
   {
       Console.WriteLine(yay);
       if (counter1 <= counter)
       {
           break;
       }
       else if (counter1 <= 1)
       {
           Console.WriteLine("please enter valid number");
       }
       counter += 1;

       continue;


   }


    }
}

}

Muhammadreal Rizwanstuff
Muhammadreal Rizwanstuff
915 Points

After trying again and again. I got this right.

using System;

namespace Treehouse.CodeChallenges { class Program { static void Main() { int counter = 0; Console.Write("Enter the number of times to print \"Yay!\": "); string yaynumber = Console.ReadLine(); var yay = "Yay!";

        try{
            var counter1 = int.Parse(yaynumber);
    while (counter1 <= 0)
    {

        Console.WriteLine("You must enter a positive number.");
        break;
        }
   for (int i = 0; i <= counter; i++)
   {

       if (counter1 <= counter)
       {

           continue;
       }
       Console.WriteLine(yay.ToString());
       counter += 1;

       continue;


   }
        }
            catch(FormatException){
            Console.WriteLine("You must enter a whole number.");
            }

   Console.ReadLine();

    }
}

}