I have a problem in these code as the error message show that the m1 and 2 is unassigned local variables

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter th number ");
            int x= int.Parse(Console.ReadLine());
            RomNo(x);
            Console.ReadLine();

        }
        static void RomNo(int n)
        {
            while (n != 0)
            {
                int s = 100;
                int r = n / s;
                n %= s;
                string m, m1, m2;
                switch (s)
                {
                    case 1:
                        m = "I";
                        m1 = "V";
                        m2 = "X";
                        break;
                    case 10:
                        m = "X";
                        m1 = "L";
                        m2 = "C";
                        break;
                    case 100:
                        m = "C";
                        m1 = "D";
                        m2 = "M";
                        break;
                    default:
                        m = "M";
                        break;
                }
                for (int i = 1; i < r; i++)
                {
                    if (s == 1000)

                        if (r == 9)
                        {
                            Console.Write(m+m2);
                            break;
                        }
                    if (r == 4)
                    {
                        Console.Write(m+m1);
                        break;
                    }
                    if (r >= 5 && i < 5)
                    {
                        Console.Write(m1);
                        i += 4;
                    }
                    else
                        Console.Write(m);
                }
            }
            return;
        }
    }
}

Recommended Answers

All 5 Replies

At which line does it complain?

i think you have to assign variables like this
string m = "";m1 = "";m2 = "";
if not solved pls give line number.

string m = "";m1 = "";m2 = "";

Will never work. My C# compiler must first know the type of m1 and m2 before he will assign something to it.(Perhaps you have a more advanced one?)
Better is to use string m ="", m1="" , m2="";
Or still even better : string m = string.Empty, m1 = string.Empty, m2 = string.Empty;
>>Egypt Pharao : And still even more better would be to give meaningfull names to m,m1 and m2. What are they? I don't know. You will be asking yourself the same question when you see this code after a month or so. If m is a fountain pen pen holder(whatever...) then call your variable FountainPenHolder or something like that.

Initializing the variables is not necessary in this case following the logic you are using.

Set the values of m1 and m2 in the default: case of your switch statement and the problem will be solved.

There is no reason to initialize a variable if you are setting the values in a switch statement with a default case.

Another problem in your code:
You declare int s = 100; in your while loop.
s? anybody any idea what s is?
You never change s when you arrive at switch (s) s is still 100. So you will always execute one case option.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.