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

mateuszwolski2
mateuszwolski2
5,504 Points

C# print that many times as user asked ...

So that's the first step of this challenge. Somehow the program shows 52 "Yay's" instead of 5. I have no idea where that 52 came from ...

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {


        static void Main()
        {
            int userNumb = Console.Read();
            int checkVal = 0;

            while (true){
                checkVal += 1;
                Console.Write("Enter the number of times to print \"Yay!\": " + checkVal);
                if (checkVal == userNumb){
                    break;
                }

            }
        }
    }
}

2 Answers

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

When accepting user-input from ReadLine() the input is stored as a string. You will have to convert it into an integer using int.Parse() (check example below)

class Program
{
    static void Main(string[] args)
    {
        Console.Write("Enter the number of times to print \"Yay!\": ");
        int userNumb = int.Parse(Console.ReadLine()); // You could use int.Parse() like this or do it on the next line

        // I would use a for-loop instead of a while loop for this challenge
        for (int i = 0; i < userNumb; i++)
        {
            Console.WriteLine("Yay"); // You only want to print out the word 'Yay' x-times
        }
    }
}
mateuszwolski2
mateuszwolski2
5,504 Points

Thank's a lot Henrik! Such a basic mistake I made ... Now the code runs fine. Once again thank you !