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

Jipson Thomas
Jipson Thomas
1,502 Points

error CS0029: Cannot implicitly convert type `int' to `string'

Couldn't find the error

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            string line = Console.Read();
            try
            {
                var nolop = Int32.Parse(line);
                var i = 0;
                while(nolop > i)
                {
                    Console.Write(" \"Yay!\"");
                    i++;

                }
            }
            catch (FormatException)
            {
                Console.Write("{0} is not an integer", line);
                // Return? Loop round? Whatever.
            }

        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,108 Points

The function "Console.Read()" returns a single character, represented as an integer code value.

:point_right: You probably want "Console.ReadLine()", which returns an entire line as a string.

            string line = Console.ReadLine();

Otherwise, good job! :+1: This is one of the tougher challenges, based on the frequency of posts about it.

Happy coding!   -sp:sparkles:

Jipson Thomas
Jipson Thomas
1,502 Points

Thank you Steven. Unfortunately, still it gives me the error Bummer! Your code could not be compiled. Please click on "Preview" to view the compiler errors.

Program.cs(8,35): error CS0029: Cannot implicitly convert type int' tostring' Compilation failed: 1 error(s), 0 warnings

Steven Parker
Steven Parker
231,108 Points

That's the difference between Console.Read and Console.ReadLine. The first returns int, and the second returns string.

I guarantee that you will not see that error once you have changed that line.