bpayne111 Posted January 26, 2004 Posted January 26, 2004 (edited) using System; namespace DELETEONEDAY { class Class1 { [sTAThread] static void Main(string[] args) { char[] userInput = new char[4]; Console.WriteLine("Please input 4 numbers. (after each number press the enter key)."); for(int x = 0; x <=3; x++) { userInput[x] = (char)Console.Read(); } for(int x = 0; x <=3; x++) { Console.WriteLine(userInput[x]); } } } } this code looks simple enough... you may wonder why i posted it. I run this code expecting to input 4 chars then displaying them on the screen... It loops 4 times as expected but only prompts me for input twice?????? here's an example of what happens when i run it. <user input: 1> <user input:2> program displays 1 2 the 2nd and 3rd Console.Read statements are run by the program but it does not pause for me to input the value. i'm stumped. (if you are wondering why i have made such a simple program it's because this is actually a java program i must write for school. the results seemed so bizzare to me, i swithced over to C# and ran the same code again only to have the same results. we aren't required to use the loops but only because we haven't 'learned' them yet... i thought i was good to go but.. maybe i'm wrong) ******edit***** problem sovled ok so it works if i hit enter after i enter all 4 char... that's cool cuz it works... but why is the user prompted twice if enter is pressed after each input char? (i'm happy it's fixed but confused as to what happens when the user screws up) brandon Edited January 26, 2004 by bpayne111 Quote i'm not lazy i'm just resting before i get tired.
NK2000 Posted January 26, 2004 Posted January 26, 2004 i would suggest: use ReadLine string input = Console.ReadLine(); after that you can parse the number: private bool ParseToInt(string text, out int number) { number = 0; try { number = Int32.Parse(text); } catch { return false; } return true; } if you dont like that out and want to return an int value then this is also ok but you dont know if the parsing failed.. so number = 0 would be the alternative if the parsing failes Quote
bpayne111 Posted January 26, 2004 Author Posted January 26, 2004 the question at hand is why is the output from that code the way it is. thanks for the help though brandon Quote i'm not lazy i'm just resting before i get tired.
Administrators PlausiblyDamp Posted January 26, 2004 Administrators Posted January 26, 2004 The problem is .Read() takes the next character from the input - including The CRLF pair. e.g. If you type 1 then 2 the buffer will be userInput[0] = '1' //byte 49 userInput[1] = //byte 13 userInput[2] = //byte 10 userInput[3] = '2' //byte 50. as NK2000 suggested use readline as this handles the CRLF automatically. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
bpayne111 Posted January 26, 2004 Author Posted January 26, 2004 aha thank you .... i forgot about the lf part... thanks brandon Quote i'm not lazy i'm just resting before i get tired.
NK2000 Posted January 26, 2004 Posted January 26, 2004 i forgot someting which could matter always trim the userinput to go sure there are no hidden/invisible chars at end of line Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.