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

what is wrong with this code

I'm new plz put into simple words t

thank you

Program.cs
using System;

namespace Treehouse.CodeChallenges{
class Program 
{ 
public static void Main() {

while(true) {

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

    int num = Convert.ToInt32(Console.ReadLine());

            if(num < 0)
            {
                     Console.Write("You must enter a positive number.");
                     break;
           }


        if(num = -)
        {
        Console.Write("You must enter a positive number");


        }



                for (int i=0; i < num; i++)
                {
                    Console.Write("Yay!");
                }
                break;
               }
               catch(FormatException fe)
              {
                Console.Write("You must enter a whole number. \n");
              }
            }
       }
    }
}

1 Answer

The problem is with this part of your code:

        if(num = -)
        {
            Console.Write("You must enter a positive number");
        }

You haven't given a valid condition inside the brackets so the compiler isn't expecting to see the brackets and is throwing an error.

To check to see if the number is negative then you would use the less than operator to check is num is less than 0. Like this:

        if(num < 0)
        {
            Console.Write("You must enter a positive number");
        }

Thanks that helped a lot.