Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have created a code to get matrix input but its not accepting three digit input or two digit input.

How to make this get input as per required digit.

C#
Console.WriteLine("\t\tMATRIX TRANSPOSE\n\n");
           try
           {
               Console.Write("Enter Number of Rows: ");
               int nrows = int.Parse(Console.ReadLine());
               Console.Write("\nEnter Number of Columns: ");
               int ncolumns = int.Parse(Console.ReadLine());
           start:
               Console.WriteLine("Enter the elements of the Matrix Seperated by TAB Key   Control+C to Exit\n");
               int[,] input = new int[3, 3];
               try
               {

                   for (int row = 0; row < nrows; row++)
                   {
                       for (int column = 0; column < ncolumns; column++)
                       {
                           var key = Console.ReadKey();
                           if (key.Key != ConsoleKey.Tab && key.Key != ConsoleKey.Enter)
                               input[row, column] = Convert.ToInt32(key.KeyChar.ToString());
                           else
                               column--;
                           if (key.Key == ConsoleKey.Enter)

                               Console.WriteLine("\n\n");

                       }
                   }
               }
               catch (Exception exp)
               {
                   Console.WriteLine(exp.Message);
                   Console.ReadLine();
                   Console.Clear();
                   goto start;
               }
               Console.WriteLine();
               Console.WriteLine("\n\nMatrix Transpose");
               for (int i = 0; i < ncolumns; i++)
               {

                   for (int j = 0; j < nrows; j++)
                   {
                       Console.Write(input[j, i] + "\t");
                   }
                   Console.WriteLine("\n\n");
               }
           }
           catch (Exception exp)
           {
               Console.WriteLine("\n\nEnter Only Numbers");
           }
           Console.ReadLine();
Posted
Comments
Malli_S 17-Feb-14 2:52am    
Looking at your code you're reading only one key at a time as a input value. The Console.ReadKey() obviously reads only one key input. You'll have to use Console.ReadLine(). Or you'll have to put Console.ReadKey() in a loop.
KUMAR619 17-Feb-14 3:47am    
@Malli_S can you make it with a code.

Please use Console.ReadLine()

1 solution

OK, there are a few things here:
1) Remove the "start" label, and the "goto" - and then forget that "goto" even exists for a couple of years. By then you should know why it's a bad idea, and when to use it. In this case it is completely unnecessary and should not be used. In all my years of coding in C#, I have never once had to use "goto" - and probably neither will you...

2) Why are you using "magic numbers" that will make your application crash if I enter say 4 rows, or 6 columns?
Change this:
C#
int[,] input = new int[3, 3];

To this:
C#
int[,] input = new int[nrows, ncolumns];


3) That really doesn't work at all, does it?
What you want to do is throw away the ReadKey code, and just use ReadLine instead:
C#
for (int row = 0; row < nrows; row++)
    {
    string inp = Console.ReadLine();
    string[] values = inp.Split('\t', ',');
    if (values.Length != ncolumns)
        {
        //Report error to user then...
        row--;
        continue;
        }
    for (int column = 0; column < ncolumns; column++)
        {
        int val;
        if (!int.TryParse(values[column], out val))
            {
            // Report error to user, then...
            row--;
            break;
            }
        else
            {
            input[row, column] = val;
            }
        }
    }
That accepts values separated by commas or tabs, or a mixture of the two...
 
Share this answer
 
Comments
KUMAR619 17-Feb-14 4:45am    
@OriginalGriff Thanks for your valuable answer.
Now I have understood the concept.
OriginalGriff 17-Feb-14 4:47am    
You're welcome!
KUMAR619 17-Feb-14 4:56am    
@OriginalGriff
Another doubt

If I give 2 rows and 3 columns as input or if I press enter without pressing tab the code is not ending
OriginalGriff 17-Feb-14 4:59am    
Did you fill in the bits where I put "Report error to user then..."?
KUMAR619 17-Feb-14 5:03am    
I written some statements but not displaying a different scenario.

If you give 4 rows and 4 r columns
Then if you enter 1tab2tab3tab4tab
Error occurs.

If you pressed Enter without pressing tab or input Nothing happens its going on still I Exit Console Window

Sorry for disturbing you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900