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

Jim Conachan
Jim Conachan
2,514 Points

Cannot resolve this: Operator `>' cannot be applied to operands of type `int' and `string'

Would someone have a quick look at this code and let me know where I'm going wrong?

Look at Project.cs

Thank you.

https://w.trhou.se/n7phtv38zn

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            var total = 0;

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

                var entry = Console.ReadLine();

                if (total > entry)
                {
                    break;
                }

                else
                {
                    Console.WriteLine("Yay!");
                    total += 1;

                }

            }
        }
    }
}

2 Answers

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

Hi there, Jim! It's important to remember that when reading in input from the user from the command line, everything coming in is of type String. It could be their name, it could be "229839". But whether or not it's a number is irrelevant. It only sees it as of the data type String right now. So to run a comparison on it against an integer or float etc, we first have to turn from a string to a number. Try this line in your code:

if (total > Int32.Parse(entry))

This will take the input by the user and try to form it into a number. That way we can do mathematical operations on it and/or comparisons against other numbers.

It's also interesting to note that this is not specific to C#. Most languages I've worked with have any input coming in from the user as a type of String. Parsing numbers out of strings is something you will do in multiple languages.

Hope this helps! :sparkles:

Jim Conachan
Jim Conachan
2,514 Points

ARG! I'm so frustrated! I actually tried some variation of the INT32 cast (I think its called cast). Anyway, thank you so much!

I did a get a very nasty message after making that change, but that will be something different. I'll try and work through that before asking any further. Thanks again!