LeetCode Integer to Roman C#

I am fighting with this puzzle - there are two errors where I got stuck.

Here’s my source code

public class Solution {
    public string IntToRoman(int num) {
        Dictionary<Int32, String> romanMap = new Dictionary<int, String>
            { 1000, "M" },
            { 900, "CM" },
            { 500, "D" },
            { 400, "CD" },
            { 100, "C" },
            { 90, "XC" },
            { 50, "L" },
            { 40, "XL" },
            { 10, "X" },
            { 9, "IX" },
            { 5, "V" },
            { 4, "IV" },
            { 1, "I" },
        String res = (0).ToString();
       //this line below gives me the two errors 
        for(String index = (num.Length - 1).ToString(), last = 0; index >= 0; index--){
            String current = romanMap[num[index]];
            res += (current < last ? -current : current);
            last = current;
        return res;

Here are my errors

Line 21: Char 33: error CS1061: 'int' does not contain a definition for 'Length' and no accessible extension method 'Length' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?) (in Solution.cs)

Line 21: Char 64: error CS0029: Cannot implicitly convert type 'int' to 'string' (in Solution.cs)

I have tried applying (int).ToString or switching the Length to Count method, however I could not resolve these errors. Any hints pretty much appreciated!

You’re accepting int num as the sole argument for the function named IntToRoman.

The first thing you do with that int is try to get its length, hence the first error message. It translates to:

You’re trying to use int.Length but I don’t know what that means

(which is why when you try to minus 1 from the value of its length, it won’t work either)

Why are you trying to minus 1 from its length? The answer to that question will help us figure out what to do next.

If num is given as 100 and what you’ve written decides to work, it’ll do this:

int num = 100;
num.Length; // 3, hypothetically, but this will generate an error in reality
num.Length - 1; // 2, hypothetically
(num.Length - 1).ToString(); // "2", hypothetically

What exactly are you trying to do…?

Thank you so much - I did not know that I could approach this error like the way you have suggested. The int error has been asking me why I am trying to minus 1 from the value of its length :slight_smile:
As I was sick as I posted this question, I do not have any idea what I wanted to do either. Will try this puzzle once I feel better

1 Like