954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

A program to convert the No. to room No.

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;
        }
    }
}
Egypt Pharaoh
Light Poster
40 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

At which line does it complain?

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

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

Naik Dhiren
Newbie Poster
16 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 
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. Ifm is a fountain pen pen holder(whatever...) then call your variable FountainPenHolder or something like that.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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.

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

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.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You