Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 10th, 2007, 11:56 PM   #1
Mixmaster
Newbie
 
Mixmaster's Avatar
 
Join Date: Sep 2006
Posts: 8
Rep Power: 0 Mixmaster is on a distinguished road
School days drama

Im doing an assignment in my C# class. The idea is to make a program to takes a month ( ie 05) day ( 1-31) century ( ie 20) year (ie 07) .

It takes the date and shows what day of the week that is.

Im doing it section by section, and im stuck on the first section.

Here's how my working code is looking so far:
using System;


public class ddate
{
	
	
	public static void Main(string[] args)
	{
		/* This program will accept a date from the user
			 * and display the day of the week.
			 */
			
		//a user is inputing numbers and I can use int 
		// to verify that.  

		string month , day , century, year ; //strings will convert to int 

		Console.WriteLine("I am going to ask you a series of questions. Press enter to continue");
		Console.ReadLine();
		Console.WriteLine("This is newb code. Enter values exactly as directed. Press enter to continue");
		Console.ReadLine();      
			
                    /*Basically the program starts here.  I just need some solid
                         ways to check if the user does it right. Suggestions? */


		Console.WriteLine("Enter a two digit month. ie 08 for August");
		month = Console.ReadLine();
		Convert.ToInt32(month); 

		Console.WriteLine("Enter a day. 1 - 31 PLEASE!");
		day = Console.ReadLine(); 
		Convert.ToInt32(day);

		Console.WriteLine("Enter the century. ie the '18' in 1870. TWO DIGITS");
		century = Console.ReadLine();
		Convert.ToInt32(century);

		Console.WriteLine("Finally enter the two digit year. ie the '70' in 1870. TWO DIGITS") ;
		year = Console.ReadLine();
		Convert.ToInt32(year);

		




		   
		
}

	}

Last edited by Mixmaster; Jan 11th, 2007 at 12:01 AM. Reason: incorrect class name
Mixmaster is offline   Reply With Quote
Old Jan 11th, 2007, 12:41 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You don't say what you're stuck about. You can easily think of ways to validate the user's input. Certain months have 30 days, others have 31, and ol' February needs to know the year mod 4, mod 100, and mod 400 to reasonably determine the leap year situation.

As far as determining the day of the week, look up Zeller's congruence. Of course, if you have libraries, and you do, the time functions will do that for you. It's helpful if you're ever programming on a bare-bones system, though.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jan 11th, 2007, 3:36 AM   #3
Mixmaster
Newbie
 
Mixmaster's Avatar
 
Join Date: Sep 2006
Posts: 8
Rep Power: 0 Mixmaster is on a distinguished road
hmmm.... tell me more!
Mixmaster is offline   Reply With Quote
Old Jan 11th, 2007, 7:56 AM   #4
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,126
Rep Power: 5 lectricpharaoh will become famous soon enough
The easiest way to accomplish something like this is to use DateTime from the System namespace. First you build the string from user input, then try to create a valid DateTime instance from it using DateTime.Parse(). This will return a valid object if successful, and otherwise, it will throw an exception. If you're not comfortable using exceptions, or wish to simplify the code, you can use DateTime.TryParse() instead. This will accept an out DateTime parameter (semantically, this is just a ref parameter that doesn't need to be initialized prior to the call), and it will return a bool to indicate whether it succeeded. If it returns true, that means that the DateTime parameter has been filled in with valid information; otherwise, the string you provided could not be converted to DateTime.

There are also a whole host of methods and properties of DateTime that you can use to display or process the information however you like.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Jan 14th, 2007, 3:42 AM   #5
Mixmaster
Newbie
 
Mixmaster's Avatar
 
Join Date: Sep 2006
Posts: 8
Rep Power: 0 Mixmaster is on a distinguished road
Ok coming good. I edited my switch statement a bit. I want to fall through the switch statemement like
do case 1
do case 2..

etc.
Up till 4. How do I go about doing that? I tried goto and it wont work for me.

switch(time)
		{
					   
			case 1: System.Console.WriteLine(" Enter the century. ie 20 or 19.");
				C = System.Console.ReadLine();
				Convert.ToInt32(C);
			goto case 2;
				
				       
					case 2: System.Console.WriteLine("Enter year in YY format. ie 96") ;
					Y = System.Console.ReadLine();
					Convert.ToInt32(Y);
					goto case 3;
					
				   
					case 3: System.Console.WriteLine("Enter month in MM format. ie 03");
					M = System.Console.ReadLine();
					Convert.ToInt32(M);
					goto case 4;

					case 4: System.Console.WriteLine("Enter the two digit day of the month. ie 13");
					D = System.Console.ReadLine();
					Convert.ToInt32(D);
					break;
				}
Mixmaster is offline   Reply With Quote
Old Jan 14th, 2007, 6:54 AM   #6
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
The default behaviour of a switch statement is to fall through each case to the next one. That's why you usually have to put a "break" at the end of each case block. Remove each "goto" line and it should work as you wish.

As a rule of thumb, "goto" should never be used. I suspect there are extremely rare cases where it might be useful, but I have yet to discover any of these cases. Steer clear of goto.
Arevos is offline   Reply With Quote
Old Jan 16th, 2007, 7:49 PM   #7
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,126
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by Arevos View Post
The default behaviour of a switch statement is to fall through each case to the next one. That's why you usually have to put a "break" at the end of each case block. Remove each "goto" line and it should work as you wish.
One problem with this is C# doesn't like these fall-throughs. It allows them are when there is no code between case labels. That is,
case 0:
case 1:
  <code here>
  break;
is okay, but
case 0:
  <code here>
case 1:
  <code here>
  break;
is not. I also believe that if the case block contains an unconditional return (or more accurately, all code branches inside that case block contain a return), the compiler will accept it (since the return will prevent fall-through). Otherwise, the compiler bitches; I believe the rationale is that it's a common source of coding errors, and (to some) bad style.
Quote:
Originally Posted by Arevos
As a rule of thumb, "goto" should never be used. I suspect there are extremely rare cases where it might be useful, but I have yet to discover any of these cases. Steer clear of goto.
I wholeheartedly agree.

@OP: You might want to make it clear whether your numbers for month and day are zero-based or one-based. For example, is January zero, or one? You could look into using an enum for the months (and days of the week).

Also, your reason for using a switch block escapes me. Since the pieces of code progress in sequential order, and always fall through, what is the point? Just stick 'em in series, and ditch the switch. If your purpose is because the user doesn't always need to enter the higher-order values (ie, if they skip the year, it will use the current one, or some other defined default), then you could use a switch inside a loop, and then increment or decrement the switch expression in the loop.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick

Last edited by lectricpharaoh; Jan 16th, 2007 at 8:04 PM.
lectricpharaoh is offline   Reply With Quote
Old Jan 16th, 2007, 8:58 PM   #8
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
Offtopic on goto's:
If you ever write a lot of error checking C code, goto will become your friend. Projects like python and opencv use it a lot internally for error handling. Lets say you have a set of operations that has many steps and complex error recovery code. Rather than having a confusing nesting of conditionals you can just jump to the error cleanup whenever you hit an error.
Game_Ender is offline   Reply With Quote
Old Jan 16th, 2007, 10:03 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
ASM is made of gotos. That does not make it your friend. It is an enemy, a devil that tends to lead you into iniquity. Internal usage is of no consequence, as internal usage is the backbone of actual emitted code for the processor. At least 90% of the time, if not 99%, it is, in a higher level language, indicative of poor understanding of the usage of the language. Sure there are exceptions; there almost always are. That's why I maintain that it is not intrinsically evil.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Military School Indigno Coder's Corner Lounge 8 Dec 28th, 2006 1:58 AM
caluculate day of week given present date and number of days srinivasc_it C++ 4 Nov 4th, 2006 5:54 AM
School Project (Need Help) Digit Visual Basic 9 Apr 28th, 2006 6:20 AM
Help finding a Server for school bigguy Coder's Corner Lounge 6 Mar 17th, 2006 11:07 PM
School Days Ancient Dragon Coder's Corner Lounge 7 Aug 3rd, 2005 2:04 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC