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
-
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
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 -
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 -
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(); } } }
-
Colwin wrote:
Console.Read();
Use
Console.ReadLine()
instead :)xacc.ide - now with IronScheme support
IronScheme - 1.0 alpha 2 out now -
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(); } } }
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
-
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
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 -
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
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:
-
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:
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
-
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(); } } }
-
Colwin wrote:
Console.Read();
Use
Console.ReadLine()
instead :)xacc.ide - now with IronScheme support
IronScheme - 1.0 alpha 2 out nowBetter 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;
}