Click here to Skip to main content
15,907,001 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
I am going to write a program that will tell the user the number of days in a individual month. The program should allow the user to enter a whole number representing the month (Jan = 1, Feb = 2, etc.). After checking that this is a valid month number (i.e., between 1 and 12), program should then determine the number of days in that month two different times: once using a series of if statements and once using a switch statement.
Place the resulting number of days in two different output Labels.
NOTE: Assume that February always has 28 days.

[EDIT]

C#
private void button1_Click(object sender, EventArgs e)
       {
           int monthNo = 0;
           string[] month = new string[13];
           month[0] = "\0";
           month[1] = "January"; month[2] = "February"; month[3] = "March";
           month[4] = "April"; month[5] = "May"; month[6] = "June";
           month[7] = "July"; month[8] = "August"; month[9] = "September";
           month[10] = "October"; month[11] = "November"; month[12] = "December";

           {


               monthNo = int.Parse(monthNoText.Text);
               if (monthNo <= 0 || monthNo > 12)
               {
                   MessageBox.Show("Invalid Input!!",
                       "Must be 1 to 12!!");

               }

               {
                   switch (monthNo)
                   {
                       case 1:
                       case 3:
                       case 5:
                       case 7:
                       case 8:
                       case 10:
                       case 12:
                           switchText.Text = "31 days";
                           break;
                       case 4:
                       case 6:
                       case 9:
                       case 11:
                           switchText.Text = "30 days";
                           break;
                       case 2:
                           switchText.Text = "28 days";
                           break;
                   }

this is what i got but i need the same info in stch label and if label
Posted
Updated 17-Apr-11 14:37pm
v2
Comments
Fabio V Silva 17-Apr-11 20:44pm    
Cheers for the edit SAKryukov, my chi is still not high enough to do it :)
Oshtri Deka 18-Apr-11 3:58am    
I would add break, after every case.

You started right

C#
if (monthNo <= 0 || monthNo > 12)
{
  MessageBox.Show("Invalid Input!!", "Must be 1 to 12!!");
} else if (monthNo == 1 || monthNo == 3 ....


See if you can go from there. If this is a school assignment I really don't want to help you to much, the best thing you can learn is to think and try for yourself.
 
Share this answer
 
Comments
jdsad2008 17-Apr-11 19:54pm    
so then for the other months i put else if again?
Fabio V Silva 17-Apr-11 19:55pm    
That's right.
jdsad2008 17-Apr-11 19:55pm    
thats gonna help me out alot thanks
Fabio V Silva 17-Apr-11 19:55pm    
Your welcome.
jdsad2008 17-Apr-11 20:23pm    
thank you so much it works awesome!!!!
This code is beyond repair, in my view, due to it is based on hard-coded data. This style is not supportable. Good code never contains long switch or if statements such things are data-driven.

You could make the set of months the enumeration type and iterate through it, create array of data indexed by enumeration. For comprehensive approaches in this direction, please see my article: Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^].

There are different approaches, though.

—SA
 
Share this answer
 
Comments
Fabio V Silva 17-Apr-11 20:49pm    
It sure looked like a school assignment and that's why it looks like this...I think there's no point giving him other pointers to other material if that doesn't help him with his task...
Sergey Alexandrovich Kryukov 17-Apr-11 21:07pm    
Well, maybe. Not that I like it. I do not accept code of such quality for whatever purpose.
--SA
Fabio V Silva 17-Apr-11 21:08pm    
LOL, I agree with you, but it must be a way of teaching switch and if statements...I think we should let it go this time :)
Sergey Alexandrovich Kryukov 17-Apr-11 21:33pm    
I only evaluated it. Now -- let it go.
--SA
No one here is going to do your homework...
 
Share this answer
 
Comments
jdsad2008 17-Apr-11 19:39pm    
i have the switch statement done but the if is the problem i am having
Fabio V Silva 17-Apr-11 19:40pm    
Then post some code and explain what exactly is your problem.
jdsad2008 17-Apr-11 19:44pm    
i did underneath
jdsad2008 17-Apr-11 19:49pm    
can you see it,the problem i am having is i need an output in an using if label and one in using switch label all i can get is the switch label to work.
Try this much simplier approach

C#
bool isValid = int.TryParse(monthNoText.Text, out monthNo);

if (!isValid) {
   MessageBox.Show("Invalid Input!! Must be 1 to 12!!");
}
else {
   switch (monthNo)
   ...
   ...
}


Good luck!
 
Share this answer
 
Comments
jdsad2008 18-Apr-11 17:10pm    
Hey pong, thank you, it did work great you are awesome!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900