2 / 2
Oct 2015

Can someone look at the below code and explain to me what is wrong. I have ran the problems set input and expected output on my machine and it returns exactly how it should. I even account for strings and numbers larger than two digits.

Thanks!

using System;
using System.Collections.Generic;
 
namespace RefreshingSkills
{
   public static class Program
   {
 
      // Method To Check String Value For Contains Integer
      public static bool IsInteger(this string s)
      {
         if (String.IsNullOrEmpty(s))
         {
            return false;
         }
 
         int i;
         return Int32.TryParse(s, out i);
      }
 
      private static void Main(string[] args)
      {
         // Prevents Non Zero Exit Code Error
         try
         {
            // Stores The Value Of User Input
            List<int> number = new List<int>();
            string userInput;
 
            Console.WriteLine("Enter Numbers:");
 
            // Loop To Allow User Input Of Numbers
            do
            {
               userInput = Console.ReadLine();
 
               if ((userInput.Length > 2 || !IsInteger(userInput)) && userInput.Length != 0)
               {
                  Console.WriteLine("Enter a two digit number.");
               }
               else if (userInput.Length != 0 && IsInteger(userInput))
               {
                  number.Add(Convert.ToInt32(userInput));
               }
            } while (userInput.Length != 0);
 
            Console.WriteLine();
            Console.WriteLine("Output:");
 
            // Loop To Output Numbers To The Console
            foreach (var item in number)
            {
               if (item != 42)
                  Console.WriteLine(item);
               else
                  break;
 
            }
 
            // Hold Console App
            Console.ReadLine();
         }
         catch (Exception e)
         {
            return;
         }
         
      }
   }
}
  • created

    Oct '15
  • last reply

    Oct '15
  • 1

    reply

  • 843

    views

  • 2

    users

Everything that you output is considered for the solution. This will cause wrong answer.

Also, it's not required for you to verify the input constraints. If the problem statement states that the input will be a two digit number you can take that for granted (unless it's not correct in which case there will typically be a ton of comments stating otherwise)

Finally, you don't need to take all input and then deliver output. You can read, output, read, output, ... until you're done. It is awkward when you're testing via the console but the console is actually working with two separate streams, stdin and stdout. The judge sends all input on stdin and reads output on stdout. If you're going to continue on any judge, it is worthwhile to set up your environment so that you can redirect stdin and stdout to files for testing so that you can avoid the cumbersome console.

Cheers!