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

Christian Tisby
Christian Tisby
9,329 Points

Final code challenge C# basics

I have gone through the entire C# basics course and have reached the final, and I"m stumped.

Here's the first part:

This final code challenge will require you to use almost everything we've covered in this course. You’ll write a program to celebrate completing this course by printing “Yay!” to the screen a given number of times. Write a program that: Prompts the user to enter the number of times to enter “Yay!”. I've done this for you. Leave this as it is. Prints “Yay!” to the screen that number of times (Hint: You’ll need a loop for this. You’ll also need to keep track of how many times the loop has run). You can print them all on the same line or on separate lines.

I don't even know where to start. In the meantime, I will be going over my notes and the videos again. Please help!

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            int runningTotal = 0;
            bool keepGoing = true;


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

        }
    }
}
}

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Christian! The code you've posted thus far has 2 unused variables and one infinite loop. So, I'm going to post some steps to take here and we'll see if that helps. This will reflect my solution.

  • Prompt the user for a number
  • Read in the number from a user and assign the result to an int. You will need to parse this.
  • Set up a for loop to run from 0 to less than but not equal to the number given by the user
  • Inside the for loop print out "Yay!"

I hope this helps, but let me know if you're still stuck! :sparkles:

John Pellow
John Pellow
16,427 Points

So use a for loop, when we haven't learnt for loop yet? so go elsewhere to learn to code?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Sorry about that, Christian. You're correct. The concept of a for loop doesn't seem to have been introduced yet. You are not required to use one, it's just how I did it. I've taken quite a number of courses here at Treehouse and it's hard to keep track of when something is introduced, so please excuse my recommendation of a for loop.

This is how I did it with a regular while loop.

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");  // get the input from the user
            int numberToRun = Int32.Parse(Console.ReadLine());  // convert the number they enter to an integer
            int count = 0;  //create a counter variable

            while(count < numberToRun) //while the counter is less than the number that the user entered
            {
                Console.Write("Yay!");  // write out "Yay!"
                count++;  // increment count by one
            }
        }
    }
}

Again, I apologize for the confusion. Please bear in mind that moderators are not Treehouse staff. When you post here, you are, for the most part, addressing other students. I'm a student just like you are!

I hope this helps! :sparkles:

Steven Parker
Steven Parker
229,644 Points

If you continue learning C#, you'll learn about For Loops in the C# Objects course.

John Pellow
John Pellow
16,427 Points

wow, very fast response and excellent job. thank you kindly, Jennifer.