Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. can you figure out why does this piece of code stop every 40 iterations instesd of 20? Hint: you have to hit enter to continue

can you figure out why does this piece of code stop every 40 iterations instesd of 20? Hint: you have to hit enter to continue

Scheduled Pinned Locked Moved Clever Code
question
13 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Colwin
    wrote on last edited by
    #1

    public static void TestIsLeapYear() { int year; bool result; for (year = 1; year < 2500; year++) { result = IsLeapYear(year); Console.WriteLine("Year {0}:{1}", year, result); if (year % 20 == 0) { Console.Read(); } } }

    C L P S 4 Replies Last reply
    0
    • C Colwin

      public static void TestIsLeapYear() { int year; bool result; for (year = 1; year < 2500; year++) { result = IsLeapYear(year); Console.WriteLine("Year {0}:{1}", year, result); if (year % 20 == 0) { Console.Read(); } } }

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      It actually stops, once for each character entered, but you can't see it (Console buffering). :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

      C 1 Reply Last reply
      0
      • C CPallini

        It actually stops, once for each character entered, but you can't see it (Console buffering). :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

        C Offline
        C Offline
        Colwin
        wrote on last edited by
        #3

        Yup that is why it was so bloody frustrating i'm new to .net. i was hitting enter and that is two character for windows "\r\n" that is why it was stopping at 40 iterations not 20. ;P ;P

        C P 2 Replies Last reply
        0
        • C Colwin

          Yup that is why it was so bloody frustrating i'm new to .net. i was hitting enter and that is two character for windows "\r\n" that is why it was stopping at 40 iterations not 20. ;P ;P

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          Had you try typing "foooooooooooooo" (plus ENTER of course)? :laugh:

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

          C 1 Reply Last reply
          0
          • C CPallini

            Had you try typing "foooooooooooooo" (plus ENTER of course)? :laugh:

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

            C Offline
            C Offline
            Colwin
            wrote on last edited by
            #5

            hmmm that would have driven me off the edge i think ;P Couldn't for the life of me figure out what was going on

            1 Reply Last reply
            0
            • C Colwin

              public static void TestIsLeapYear() { int year; bool result; for (year = 1; year < 2500; year++) { result = IsLeapYear(year); Console.WriteLine("Year {0}:{1}", year, result); if (year % 20 == 0) { Console.Read(); } } }

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              Colwin wrote:

              Console.Read();

              Use Console.ReadLine() instead :)

              xacc.ide - now with IronScheme support
              IronScheme - 1.0 alpha 2 out now

              C N 2 Replies Last reply
              0
              • L leppie

                Colwin wrote:

                Console.Read();

                Use Console.ReadLine() instead :)

                xacc.ide - now with IronScheme support
                IronScheme - 1.0 alpha 2 out now

                C Offline
                C Offline
                Colwin
                wrote on last edited by
                #7

                yup i use that now. Had me buggered for a while there but :-D

                1 Reply Last reply
                0
                • C Colwin

                  public static void TestIsLeapYear() { int year; bool result; for (year = 1; year < 2500; year++) { result = IsLeapYear(year); Console.WriteLine("Year {0}:{1}", year, result); if (year % 20 == 0) { Console.Read(); } } }

                  P Offline
                  P Offline
                  Paul Conrad
                  wrote on last edited by
                  #8

                  Console.Read is the culprit?

                  "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                  C 1 Reply Last reply
                  0
                  • C Colwin

                    Yup that is why it was so bloody frustrating i'm new to .net. i was hitting enter and that is two character for windows "\r\n" that is why it was stopping at 40 iterations not 20. ;P ;P

                    P Offline
                    P Offline
                    peterchen
                    wrote on last edited by
                    #9

                    You have a similar effect with getch() popular with Wintel compilers: Special keys (eg. F1..F10) send two chars - a zero and a scan code. A similar loop would result in the same problem - not as pronounced since it affects less common "go on" keys.

                    We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                    blog: TDD - the Aha! | Linkify!| FoldWithUs! | sighist

                    1 Reply Last reply
                    0
                    • P Paul Conrad

                      Console.Read is the culprit?

                      "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                      C Offline
                      C Offline
                      Colwin
                      wrote on last edited by
                      #10

                      Yup Console.Read() is the culprit.. since enter is two characters a carriage return and a line feed it reads one of them and in the next loop iteration reads the next one..so it stops every 40 iterations when you hit the enter key.. :sigh: i checked everything from the mod operator to the equal to operator documentation..then i read the doc for the Read() function.. Classic case of RTFM :doh:

                      P 1 Reply Last reply
                      0
                      • C Colwin

                        Yup Console.Read() is the culprit.. since enter is two characters a carriage return and a line feed it reads one of them and in the next loop iteration reads the next one..so it stops every 40 iterations when you hit the enter key.. :sigh: i checked everything from the mod operator to the equal to operator documentation..then i read the doc for the Read() function.. Classic case of RTFM :doh:

                        P Offline
                        P Offline
                        Paul Conrad
                        wrote on last edited by
                        #11

                        Colwin wrote:

                        Classic case of RTFM

                        Yep. It just happens :-D

                        "I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon

                        1 Reply Last reply
                        0
                        • C Colwin

                          public static void TestIsLeapYear() { int year; bool result; for (year = 1; year < 2500; year++) { result = IsLeapYear(year); Console.WriteLine("Year {0}:{1}", year, result); if (year % 20 == 0) { Console.Read(); } } }

                          S Offline
                          S Offline
                          soqu
                          wrote on last edited by
                          #12

                          ha,it is just like a trick.Interesting~~ :laugh:

                          1 Reply Last reply
                          0
                          • L leppie

                            Colwin wrote:

                            Console.Read();

                            Use Console.ReadLine() instead :)

                            xacc.ide - now with IronScheme support
                            IronScheme - 1.0 alpha 2 out now

                            N Offline
                            N Offline
                            ncarey
                            wrote on last edited by
                            #13

                            Better to add to your toolbox of utility/helper methods:

                            static bool WaitForKeyPress()
                            {
                            return WaitForKeyPress( ConsoleKey.Escape ) ;
                            }

                            static bool WaitForKeyPress( ConsoleKey quit_key )
                            {
                            ConsoleKeyInfo keypress ;
                            bool fQuit = false ;

                            FlushConsoleInputBuffer() ;
                            WriteConditionalEOL() ;
                            Console.Write( "Press ESC to quit or any other key to continue> " ) ;

                            keypress = Console.ReadKey( false );
                            fQuit = ( keypress.Key == quit_key ? true : false );

                            WriteConditionalEOL();

                            return fQuit;

                            }

                            private static void FlushConsoleInputBuffer()
                            {
                            while ( Console.KeyAvailable )
                            {
                            Console.ReadKey( true ); // true: don't echo input
                            }
                            return;
                            }

                            private static void WriteConditionalEOL()
                            {
                            if ( Console.CursorLeft > 0 )
                            {
                            Console.WriteLine();
                            }
                            return;
                            }

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups