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#

Hammad Ali
PLUS
Hammad Ali
Courses Plus Student 2,610 Points

I'm stuck in c# code!

Question is Add more input validation to your program by printing “You must enter a positive number.” if the user enters a negative number.

This is the code I typed but it is not accepting. Whenever I enter check code it displays "I entered -1. I expected "You must enter a positive number.", but got "You must enter a whole number. You must enter a positive number." instead.". Help me with this where I'm doing mistake.

using System;

namespace Treehouse.CodeChallenges { class Program { static void Main() {
while(true) { try { Console.Write("Enter the number of times to print \"Yay!\": "); Console.Write("You must enter a whole number. \n"); int num = Convert.ToInt32(Console.ReadLine());

                if(num < 0)
                {
                    Console.Write("You must enter a positive number.");
                    num = Convert.ToInt32(Console.ReadLine());
                }
                int i=0;
                for ( i=0; i < num; i++)
                {
                    Console.Write("Yay!");
                }
                break;
            }
            catch(FormatException fe)
            {
                Console.Write("Invalid entry");
            }

    }

    }
}

}

6 Answers

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;
           }


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

Test if this works pls since you dont linked your challange :)

william williams
william williams
2,912 Points
using System;

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

            Console.Write("Enter the number of times to print \"Yay!\": ");
            var entry = Console.ReadLine();
            int counter  = 0;

            try
            {
            var total = int.Parse(entry);

            if(total < 0)
            {
                Console.WriteLine("You must enter a positive number");
            }
                else
                {
                    while (total > counter)
                    {
                      Console.WriteLine("Yay!");
                      counter +=1;
                    }
                }
            }

            catch(FormatException)
            {
            Console.WriteLine("You must enter a whole number");
            }
            Console.ReadLine();
        }
    }
}

Well I tried....Okay?!

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            string input = Console.ReadLine();
            int i = 0;
            try
            {
                int count = int.Parse(input);
                if(count < 0)
                {
                    Console.WriteLine("You must enter a positive number.");
                }
                else
                {
                        while(i < count)
                        {
                            i += 1;   
                            Console.WriteLine("Yay!");
                        }
                    }
                }
            catch(FormatException)
                {
                    Console.WriteLine("You must enter a whole number.");
                }
        }
    }
}
jas cheema
jas cheema
823 Points

using System;

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

        string entry = Console.ReadLine();
        try
        {

            int total = Convert.ToInt32(entry);

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


            }

            else
            {
                for (int i = 0; i < total; i++)
                {
                    Console.WriteLine("Yay!");
                }
            }


        }
        catch (FormatException)
        {
            Console.WriteLine("You must enter a whole number");
        }

    }
}

}

Hope this helps,

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {               
            while(true)
            {   
                try
                {                    
                    Console.Write("Enter the number of times to print \"Yay!\": ");
                    int input = Convert.ToInt32(Console.ReadLine()); 

                    if (input < 0)
                    {                         
                        Console.WriteLine("You must enter a positive number."); 
                        break;
                    }
                    else    
                    {
                        for(int i = 0; i < input; i++)
                        {
                            Console.Write("Yay!");
                        }
                        break;
                    }
               }     
                catch(FormatException)
                {
                    Console.WriteLine("You must enter a whole number.");
                    continue;
                }

            }

        }
    }
}
HIDAYATULLAH ARGHANDABI
HIDAYATULLAH ARGHANDABI
21,058 Points
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {               
            while(true)
            {   
                try
                {                    
                    Console.Write("Enter the number of times to print \"Yay!\": ");
                    int input = Convert.ToInt32(Console.ReadLine()); 

                    if (input < 0)
                    {                         
                        Console.WriteLine("You must enter a positive number."); 
                        break;
                    }
                    else    
                    {
                        for(int i = 0; i < input; i++)
                        {
                            Console.Write("Yay!");
                        }
                        break;
                    }
               }     
                catch(FormatException)
                {
                    Console.WriteLine("You must enter a whole number.");
                    continue;
                }

            }

        }
    }
}