Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Can someone please check the code below and mention the problem area.

C#
<pre>static void Main(string[] args)
        {
            Console.Write("How many nums to be compared: ");
            int n = int.Parse(Console.ReadLine()); //Parse int n for arr.Length
            int[] arr = new int[n]; //Creates array with name arr and length n
            Console.WriteLine("Enter the nums: "); //To be used while entering elements

            //Registers array elements
            for (int arrIndex = 0; arrIndex < arr.Length; arrIndex++)
            {
                arr[arrIndex] = int.Parse(Console.ReadLine()); //Parse array elements
            }

            //Prints array
            for (int arrIndex = 0; arrIndex < arr.Length; arrIndex++)
            {
                Console.Write(arr[arrIndex] + " ");
            }
            Console.WriteLine();

            GetMax(arr);
        }

        static void GetMax(int[] array)
        {
            if ((array[0]) > (array[1]))
            {
                Console.Write("Number {} is > than {}", array[0], array[1]);
            }
            else if ((array[0]) < (array[1]))
            {
                Console.Write("Number {} is < than {}", array[0], array[1]);
            }
            else
            {
                Console.Write("Number {} and {} are equal", array[0], array[1]);
            }
        }}


What I have tried:

I tried re-reading the complete chapter on methods from the book I'm learning C# but couldn't find any reason.

I'm an absolute beginner so I request to please keep the explanation very very simple.

It gives me the following error when I run the code

How many nums to be compared: 2
Enter the nums:
1
5
1 5

Unhandled Exception: System.FormatException: Input string was not in a correct format.
   at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
   at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
   at System.IO.TextWriter.Write(String format, Object arg0, Object arg1)
   at System.IO.TextWriter.SyncTextWriter.Write(String format, Object arg0, Object arg1)
   at System.Console.Write(String format, Object arg0, Object arg1)
   at Ch9ExQ2.Program.GetMax(Int32[] array) in C:\Users\mohit\source\ProgFundamExrsQs\Ch9ExQ2\Ch9ExQ2\Program.cs:line 42
   at Ch9ExQ2.Program.Main(String[] args) in C:\Users\mohit\source\ProgFundamExrsQs\Ch9ExQ2\Ch9ExQ2\Program.cs:line 31
Press any key to continue . . .
Posted
Updated 23-Sep-18 20:07pm

Your problem is in the Console.Write function - you forgot to put the index for the variable to be shown in the output. You should also add a Console.ReadKey(); call at the end of the Main method because you will, otherwise, not see the output when you start the program from Visual Studio...

C#
static void GetMax(int[] array)
        {
            if ((array[0]) > (array[1]))
            {
                Console.Write("Number {0} is > than {1}", array[0], array[1]);
            }
            else if ((array[0]) < (array[1]))
            {
                Console.Write("Number {0} is < than {1}", array[0], array[1]);
            }
            else
            {
                Console.Write("Number {0} and {1} are equal", array[0], array[1]);
            }
        }
 
Share this answer
 
Comments
Graeme_Grant 24-Sep-18 2:09am    
Countered the 1 vote
Dirk Bahle 24-Sep-18 4:00am    
Thanks, strange - I cannot see a reason for voting this with 1 star?
Graeme_Grant 24-Sep-18 9:59am    
exactly
Dirk Bahle 27-Sep-18 10:06am    
It looks like it happened again:
https://www.codeproject.com/Answers/1261498/Best-way-to-design-my-classes-for-the-requirement#answer2

This makes me wonder if this is the same person maybe trying to resolve a personal issue with me or CP ...?
Graeme_Grant 27-Sep-18 17:42pm    
yep
Thanks for pointing that out.

Next time i will be more attentive while rechecking my codes.

Thanks again. Yes the program is working now.
 
Share this answer
 
Comments
Richard Deeming 25-Sep-18 12:14pm    
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution.

DO NOT post your comment as a new "solution".

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