3

This is the first time I am getting NZEC error. I don't understand this error, I just found that it stands for "Non Zero Exit Code". But what does this means. When we get this error. I am getting this error on one of online coding challenge websites and not in Visual Studio. I am real need of help. Please check following code snippet where I am getting this error and please suggest what I am doing wrong.

using System;
using System.Collections.Generic;
using System.Linq;

namespace PractiseConsoleApplication
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            //Get input
            var stringInput = Console.ReadLine();

            var numberOfRooms = Convert.ToInt32(Console.ReadLine());

            var endOfInput = Convert.ToInt32(Console.ReadLine());

            var customers = stringInput.ToArray();

            var hotel = new Hotel(numberOfRooms);

            foreach (var customerName in customers)
            {
                hotel.CheckInCheckoutCustomer(customerName);
            }

            Console.WriteLine(Convert.ToString(hotel.CustomersLeftWithoutStayingInHotel.Count));
        }
    }

    internal class Hotel
    {
        public bool IsRoomAvailable { get; private set; }

        public List<HotelRoom> Rooms { get; private set; }

        public List<char> CustomersLeftWithoutStayingInHotel { get; private set; }

        private Hotel()
        {
            Rooms = new List<HotelRoom>();
            CustomersLeftWithoutStayingInHotel = new List<char>();
        }

        public Hotel(int numberOfRooms)
            : this()
        {
            for (int i = 1; i <= numberOfRooms; i++)
            {
                Rooms.Add(new HotelRoom(i));
            }
        }

        public void CheckInCheckoutCustomer(char customer)
        {
            if (CustomersLeftWithoutStayingInHotel.Any(f => f == customer))
            {
                return;
            }

            var existingCustomer = Rooms.FirstOrDefault(f => f.IsRoomAllocatedToCustomer  && f.CustomerName == customer);

            //Already room allocated to this customer
            if (existingCustomer != null)
            {
                //checkout him
                existingCustomer.CustomerCheckout();
            }
            else
            {
                //Get empty rooms
                var emptyRoom = Rooms.FirstOrDefault(f => f.IsRoomAllocatedToCustomer == false);

                if (emptyRoom != null)
                {
                    emptyRoom.AllocateRoomToCustomer(customer);
                }
                else
                {
                    CustomersLeftWithoutStayingInHotel.Add(customer);
                }
            }
        }
    }

    internal class HotelRoom
    {
        public int RoomNumber { get; private set; }

        public char? CustomerName { get; private set; }

        public HotelRoom(int roomNumber)
        {
            RoomNumber = roomNumber;
            CustomerName = null;
        }

        public bool IsRoomAllocatedToCustomer
        {
            get
            {
                return CustomerName.HasValue;
            }
        }

        public void AllocateRoomToCustomer(char customerDetails)
        {
            CustomerName = customerDetails;
        }

        public void CustomerCheckout()
        {
            CustomerName = null;
        }
    }

    internal class Customer
    {
        public char CustomerName { get; private set; }

        public Customer(char name)
        {
            CustomerName = name;
        }
    }
}
Nikhil Chavan
  • 1,685
  • 2
  • 20
  • 34
  • 1
    Indicate what line of code the error is occurring in for starters. – dbugger Dec 20 '14 at 12:10
  • Where is this code running/being run? – Chris Dec 20 '14 at 12:11
  • @dbugger This is what i am trying to find where is error. It is very simple program. Chris - I am practising on HackerEarth website. When I submit this code I get this error. – Nikhil Chavan Dec 20 '14 at 12:13
  • Potentially relevant: http://stackoverflow.com/questions/5436207/runtime-error-nzec-in-simple-code I'd be inclined to run the code in something like Linqpad to see if the result is what you'd expect. – Chris Dec 20 '14 at 12:16
  • try commenting statements and sending code to HackerEarth, when the error disappear, that statement will be the wrong one. – niceman Dec 20 '14 at 12:22
  • Strange one, first thought would be change main to return an int instead of void. See if that tells you anything. – Tony Hopkinson Dec 20 '14 at 12:24

1 Answers1

0

as Tony Hopkinson says in the comments, main should return an int.
place this statement in the end of main method:

return 0;

this will exit with zero code(thus the Non Zero Exit Code error)

niceman
  • 2,653
  • 29
  • 57
  • understand that main in many languages should always return an int indicating that nothing went wrong(return 0) or that something did(return Non zero) – niceman Dec 20 '14 at 12:56
  • 1
    But, but, but ... you can't say "return 0;" in a method that returns void, the compiler won't accept that. And I can't believe that this is the problem, I've written hundreds of programs that said "void Main()" and "return;" and never gotten the error the OP is reporting. – RenniePet Dec 20 '14 at 13:59
  • This is the second time today I've noticed that you've posted an answer that is basically just a rewrite (and in this case an incorrect rewrite - you forgot to mention changing the Main() method) of a suggestion in the comments. http://stackoverflow.com/questions/27576166/adding-assembly-reference-to-quartztypelib I don't see this as very constructive. – RenniePet Dec 20 '14 at 14:08
  • @RenniePet I sayed "as Tony Hopkinson says in the comments", I didn't mention you should change main return type because tony said it,and even if it works with you it may not work on another device. – niceman Dec 20 '14 at 15:15
  • @RenniePet and what's that question doing in your comment – niceman Dec 20 '14 at 15:16
  • @user2397162 - sadly, it didn't worked. But that's fine, running the code is not aim. I was curious about this new type of error to me. And a lot of people directed me. Thanks to all :). – Nikhil Chavan Dec 20 '14 at 17:30