binod km

binod km

  • NA
  • 5
  • 5.3k

C# compiler inserting '/r' and '/n' chars while reading consecutive input lines for a char array.

Oct 22 2012 7:02 AM

hi there......please run this C# code block. you can see that the words are not read and stored correctly in the char array. Begining with the end of first word(first line), instead of reading the first letter of the next word/line, the compiler inserts characters '/r' and '/n' into the first two positions of the corresponding row of the array.can any body tell how to solve this. Thanks

static void Main(string[] args)
        {
            char[,] arr=new char[4,4];
            Console.WriteLine("enter 4 four charcter words in seperate lines.\nthat is press enter after each                 word");

            //saving in an 2D array
            for(int i=0;i<4;i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    arr[i, j] = Convert.ToChar(Console.Read());
                }
            }

           //displaying 2D array
            for(int i=0;i<4;i++)
           {
               for (int j = 0; j < 4; j++)
                {
                   Console.Write("{0}\t",arr[i,j]);
                }
               Console.WriteLine();
           }
           Console.ReadLine();

 }


Answers (2)