| | |
A program to convert the No. to room No.
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 17
Reputation:
Solved Threads: 0
I have a problem in these code as the error message show that the m1 and 2 is unassigned local variables
C# Syntax (Toggle Plain Text)
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; } } }
•
•
•
•
Originally Posted by Naik Dhiren
string m = "";m1 = "";m2 = "";
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
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.
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.
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
![]() |
Similar Threads
- Pointers (archived tutorial) (C++)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Data validation- want only Int's (C++)
- Problem with memory allocation =( (C)
- Convert my old VHS to DVD (USB Devices and other Peripherals)
- IF I were to convert to the Dark Side... (IT Professionals' Lounge)
- LC3 Calculator (Assembly)
- Cannot open REGEDIT !!! (Windows NT / 2000 / XP)
- Pointers (C++)
Other Threads in the C# Forum
- Previous Thread: Getting the next Form No
- Next Thread: Trouble with combo boxes
| Thread Tools | Search this Thread |
.net access ado.net algorithm appportability array avltree barchart bitmap box broadcast brush buttons c# check checkbox client color combobox control conversion csharp custom customactions database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum error event excel file form format forms function gdi+ http httpwebrequest image index input install java label list listbox mailmerge mandelbrot math mouseclick mysql operator path photoshop picturebox pixelinversion pixels post programming radians regex remote remoting resolved. richtextbox rotation serialization server sleep socket sockets sql statistics stream string table text textbox thread time timer transparency update usercontrol validation virtualization visualstudio webbrowser windows winforms wpf xml






