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

Can I use the int value of a variable in the condition in a for loop? I keep getting the error that <= cannot be used

Error message: Operator <=' cannot be applied to operands of typeint' and `string'

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");

            var e = Console.ReadLine();

            for (var n = 1; n <= e; n++)
            {
                Console.Write("Yay!");
            }

        }
    }
}

1 Answer

Stuart Wright
Stuart Wright
41,119 Points

I don't know C# but from the looks of that error message I'm thinking that e is most likely still a string and you can't compare a string to an integer. I think converting e to an integer before the loop starts is probably what you need to do.