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 trialJipson Thomas
1,502 Pointserror CS0029: Cannot implicitly convert type `int' to `string'
Couldn't find the error
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
231,261 PointsThe function "Console.Read()
" returns a single character, represented as an integer code value.
You probably want "Console.ReadLine()
", which returns an entire line as a string.
string line = Console.ReadLine();
Otherwise, good job! This is one of the tougher challenges, based on the frequency of posts about it.
Happy coding! -sp
Jipson Thomas
1,502 PointsJipson Thomas
1,502 PointsThank 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' to
string' Compilation failed: 1 error(s), 0 warningsSteven Parker
231,261 PointsSteven Parker
231,261 PointsThat'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.