943,899 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 424
  • C# RSS
Feb 26th, 2009
0

A program to convert the No. to room No.

Expand Post »
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)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace ConsoleApplication1
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. Console.WriteLine("Enter th number ");
  12. int x= int.Parse(Console.ReadLine());
  13. RomNo(x);
  14. Console.ReadLine();
  15.  
  16. }
  17. static void RomNo(int n)
  18. {
  19. while (n != 0)
  20. {
  21. int s = 100;
  22. int r = n / s;
  23. n %= s;
  24. string m, m1, m2;
  25. switch (s)
  26. {
  27. case 1:
  28. m = "I";
  29. m1 = "V";
  30. m2 = "X";
  31. break;
  32. case 10:
  33. m = "X";
  34. m1 = "L";
  35. m2 = "C";
  36. break;
  37. case 100:
  38. m = "C";
  39. m1 = "D";
  40. m2 = "M";
  41. break;
  42. default:
  43. m = "M";
  44. break;
  45. }
  46. for (int i = 1; i < r; i++)
  47. {
  48. if (s == 1000)
  49.  
  50. if (r == 9)
  51. {
  52. Console.Write(m+m2);
  53. break;
  54. }
  55. if (r == 4)
  56. {
  57. Console.Write(m+m1);
  58. break;
  59. }
  60. if (r >= 5 && i < 5)
  61. {
  62. Console.Write(m1);
  63. i += 4;
  64. }
  65. else
  66. Console.Write(m);
  67. }
  68. }
  69. return;
  70. }
  71. }
  72. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Egypt Pharaoh is offline Offline
40 posts
since Dec 2008
Feb 26th, 2009
0

Re: A program to convert the No. to room No.

At which line does it complain?
Last edited by LizR; Feb 26th, 2009 at 7:05 am.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Feb 26th, 2009
0

Re: A program to convert the No. to room No.

i think you have to assign variables like this
string m = "";m1 = "";m2 = "";
if not solved pls give line number.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Naik Dhiren is offline Offline
16 posts
since Nov 2008
Feb 26th, 2009
0

Re: A program to convert the No. to room No.

Quote originally posted by Naik Dhiren ...
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.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Feb 26th, 2009
0

Re: A program to convert the No. to room No.

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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Feb 26th, 2009
0

Re: A program to convert the No. to room No.

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.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Getting the next Form No
Next Thread in C# Forum Timeline: Trouble with combo boxes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC