Shorter one

Jan 7, 2014 at 12:59 PM
Edited Jan 7, 2014 at 4:39 PM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic;


namespace RomanConverter
{
class RomanNumeralConverter
{
    static void Main(string[] args)
    {
        int convertThisNumber;  //Stores value to convert
        string convertedValue;  //Stores RomanNumeral Value
        bool PlayAgain = true; 

        //Loop to run until user quits
        while (PlayAgain == true)
        {

            Console.WriteLine("\nEnter a number to convert, or press Q to quit:  \n");

            string inputKey;
            inputKey = Console.ReadLine();
            if (inputKey == "Q" || inputKey == "q") { PlayAgain = false; break; }
            if (Information.IsNumeric(inputKey)) //IsNumeric function from VB library
            {
                convertThisNumber = Convert.ToInt32(inputKey);
                convertedValue = ConvertToRoman(convertThisNumber);
                Console.WriteLine("\n" + convertThisNumber + " written as a roman numeral is: " + convertedValue + "\n" + "\n");
                convertThisNumber = 0;
            }
            else
            {
                Console.WriteLine("\nYou typed an invalid entry.");
            }

        }
    }



    private static string ConvertToRoman(int convertThis)
    {
        int leftovers;              //store mod results
        string RomanNumeral = "";   //store roman numeral string
        Dictionary<string, int> dict = new Dictionary<string, int>()
        {
            {"M", 1000},// 1000 = M
            {"CM", 900},// 900 = CM
            {"D", 500}, // 500 = D
            {"CD", 400},// 400 = CD
            {"C", 100}, // 100 = C
            {"XC", 90}, // 90 = XC
            {"L", 50},  // 50 = L
            {"XL", 40}, // 40 = XL
            {"X", 10},  // 10 = X
            {"IX", 9},  // 9 = IX
            {"V", 5},   // 5 = V
            {"IV", 4},  // 4 = IV
            {"I", 1},   // 1 = I
        };
        foreach (KeyValuePair<string, int> pair in dict)
        {
            if (convertThis >= pair.Value)
            {
                leftovers = convertThis % pair.Value;
                int remainder = (convertThis - leftovers) / pair.Value;
                convertThis = leftovers;
                while (remainder > 0)
                {
                    RomanNumeral += pair.Key; remainder--;
                }
            }
        }
        return RomanNumeral;
    }
}
}