Thread: Another beginner question

  1. #1
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537

    Another beginner question

    Help! I'm new to C# and keep getting stuck on things which should be pretty simple

    Can anyone tell me why this loop repeats the output found in the menu( ) 3 times when a number is entered?

    Code:
    class Tester
    	{
    		private const int MAX_BOOKS = 5;
    		static private Book[] library = new Book[ MAX_BOOKS ];
    /*
    		Book[] Library
    		{
    			get
    			{
    				return library;
    			}
    		}
    */
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		/// 
    		
    
    
    		static int menu( )
    		{
    			Console.WriteLine( "There are {0} books in the library", library.Length );
    			Console.WriteLine( "Please enter and option:" );
    			Console.WriteLine( "1. Enter book details" );
    			Console.WriteLine( "2. Display book details" );
    			Console.WriteLine( "5. Exit" );
    			return Console.Read( );
    		}
    
    		[STAThread]
    		static void Main( )
    		{
    			int choice;
    
    			do
    			{
    				choice = menu( );
    
    				switch( choice )
    				{
    					case '1':
    						for( int i = 0; i < library.Length; i++ )
    						{
    							
    						}		
    						break;
    
    					case '5':
    						Console.WriteLine( "Exiting...." );
    						break;
    					
    					default:
    						Console.WriteLine( "Invalid choice. Please try again." );
    						break;
    				}
    			}while( choice != '5' );
    		}
    	}
    Also, while on the subject of console programs what is the best way to clear the console?
    Couldn't think of anything interesting, cool or funny - sorry.

  2. #2
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Console.Read() is reading newlines.

    Try Console.ReadKey() instead.
    Code:
    	class Test
    	{
    		static ConsoleKeyInfo menu()
    		{
    			Console.WriteLine("Please enter and option:");
    			return Console.ReadKey();
    		}
    
    		[STAThread]
    		static void Main()
    		{
    			bool quit = false;
    			do
    			{
    				switch (menu().KeyChar)
    				{
    					case '1':
    						Console.WriteLine("You pressed 1");
    						break;
    
    					case '5':
    						Console.WriteLine("You pressed 5");
    						quit = true;
    						break;
    
    					default:
    						Console.WriteLine("You pressed something else");
    						break;
    				}
    			} while (!quit);
    		}
    	}

    Also, to clear the console use Console.Clear()

  3. #3
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Quote Originally Posted by BMJ
    Also, to clear the console use Console.Clear()

    Thanks for that, I'll try i tlater when I get a chance. Must've been blind not to Clear( ) in the class description!
    Couldn't think of anything interesting, cool or funny - sorry.

  4. #4
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    why does my compiler not recognise Console.Clear or Console.ReadKey??
    Couldn't think of anything interesting, cool or funny - sorry.

  5. #5
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Quote Originally Posted by endo
    why does my compiler not recognise Console.Clear or Console.ReadKey??
    You're not using Visual C#?

    Framework v1.1 or beta 2.0?

  6. #6
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Is this likely to be the problem, my visual c# may be a little out of date - i been away from coding for a bit

    .net framework 1.0 version 1.0.3705
    Couldn't think of anything interesting, cool or funny - sorry.

  7. #7
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Wow, I'm very sorry I should have mentioned this, Console.Clear() and Console.ReadKey() are Framework version 2 members.

    I've become used to version 2 already.


    You could always download http://lab.msdn.microsoft.com/expres...p/default.aspx if you're willing to use the beta Framework.
    Last edited by BMJ; 03-31-2005 at 12:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: Linked List question
    By WeatherMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 07:16 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM
  3. beginner question
    By Barrot in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2005, 02:17 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 07:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 08:39 PM