Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class LargestofThree
    {
       
        static void Main(string[] args)
        {
        
        
        int _firstNumber, _secondNumber, _thirdNumber;


        Console.WriteLine("Enter your 3 numbers to compare");


        _firstNumber = int.Parse(Console.ReadLine());
        _secondNumber = int.Parse(Console.ReadLine());
        _thirdNumber = int.Parse(Console.ReadLine());

        if (_firstNumber>_secondNumber && _firstNumber>_thirdNumber)
        {
            Console.WriteLine("The Greatest of 3 numbers is {0}",_firstNumber);
        }
        else if (_secondNumber>_firstNumber && _secondNumber>_thirdNumber)
        {
            Console.WriteLine("The Greatest of 3 numbers is {1}",_secondNumber);
        }
        else if (_thirdNumber>_firstNumber &&_thirdNumber>_secondNumber)
        {
           Console.WriteLine("The Greatest of 3 numbers is {2}",_thirdNumber);
        }



        }
    }
}

when i run the program i get a error in the last else-if statment

"Unhandled Exception: System.FormatException: Index (zero based) must be greater
than or equal to zero and less than the size of the argument list."
Posted
Updated 1-Mar-14 14:08pm
v2

1 solution

When you use the {#n} substitution place-holder in a string, there must be a number of arguments, after the string, which correspond to the zero-based index number inside the {}.

When you write: Console.WriteLine("The Greatest of 3 numbers is {1}",_secondNumber);

The compiler looks for a second argument after the string (index = #1), and there is an error.

The remedy is simple: just use {0} in all your statements that write to the Console.

I notice you reference the Linq library in your code; do you want to know how to simplify this using Linq ? Hint: put your three numeric values in a List, and use the Max extension method of IEnumerable.
 
Share this answer
 
Comments
ShaHam11 1-Mar-14 20:46pm    
Thank you Billwoodruff for the solution. I changed the place-holder to {0} on every console statement.. I just wanted to re-phrase from my understanding from your comment why there is an error in my program. My program gives error in 3rd elseif statement i.e _firstnumber=10,_second number=20 & _thirdnumber=30.. it gives me exception error but where has if i give the _thirdnumber=5 and then compile and run the program it works fine...So you explained the compiler is trying to look for the second or third argument respectively and it finds number of arguments not equal to number of place holders hence it errors out..
BillWoodruff 1-Mar-14 21:49pm    
You got it, ShaHam, for every use of {#n} inside the string used as an argument to Console.WriteLine, there must be an item in the comma-separated list of parameters that follows the string which evaluates to Type 'string; that corresponding item is then "injected" into the string when the code is executed.

Console.WriteLine("The hypotenuse of a triangle with sides equal to #{0}, and #{1}, is #{2}", side1.ToString(), side2.ToString(), Math.Sqrt(Math.Pow(side1, 2) + Math.Pow(side2, 2)).ToString());

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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