A program to convert the No. to room No.

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 17
Reputation: Egypt Pharaoh is an unknown quantity at this point 
Solved Threads: 0
Egypt Pharaoh Egypt Pharaoh is offline Offline
Newbie Poster

A program to convert the No. to room No.

 
0
  #1
Feb 26th, 2009
I have a problem in these code as the error message show that the m1 and 2 is unassigned local variables
  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

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

 
0
  #2
Feb 26th, 2009
At which line does it complain?
Last edited by LizR; Feb 26th, 2009 at 7:05 am.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 16
Reputation: Naik Dhiren is an unknown quantity at this point 
Solved Threads: 0
Naik Dhiren Naik Dhiren is offline Offline
Newbie Poster

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

 
0
  #3
Feb 26th, 2009
i think you have to assign variables like this
string m = "";m1 = "";m2 = "";
if not solved pls give line number.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,955
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 282
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

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

 
0
  #4
Feb 26th, 2009
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,224
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 574
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

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

 
0
  #5
Feb 26th, 2009
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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,955
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 282
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

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

 
0
  #6
Feb 26th, 2009
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC