I've been trying to run a program which asks a user for coordinates of several lines. The code then uses pythagoras to calculate the length of these lines. However, the problem that I'm having is when a user enters the first coordinate and presses enter, they get no opportunity to enter the remaining ones. It's alomost as though whne the enter button is pressed, the program thinks it has been hit more than once and skips the remaining coordinate input.
I don't think it's the code - I think it may be the C# Express editor. If so, is there anyway to alter the 'Enter' key sensitivity
I'm open to suggestions.
Here's the code:
using
System;struct
Point{
private byte x; private byte y; public byte xVal{
get { return x; } set { x = value; }}
public byte yVal{
get { return y; } set { y = value; }}
}
struct
Line{
private Point starting; private Point ending; public static double longest = 0; private static byte firstLoad = 0; public Point start{
get { return starting; } set { starting = value; }}
public Point end{
get { return ending; } set { ending = value; }}
public double length(){
double len = 0;len = System.
Math.Sqrt((end.xVal - start.xVal) * (end.xVal - start.xVal) + (end.yVal - start.yVal) *(end.yVal - start.yVal));
return len;}
}
class
LineApp{
public static void Main(){
Line myLine = new Line(); Point myPoint = new Point();int buffer; for (byte max = 0; max < 10; max++, counter++)
{
Console.Write("Enter the Start x coordinate of the line and press enter: ");buffer =
Console.Read();myPoint.xVal = (
byte) buffer; Console.Write("Enter the Start y coordinate of the line and press enter: ");buffer =
Console.Read();myPoint.yVal = (
byte) buffer;myLine.start = myPoint;
Console.Write("Enter the End x coordinate of the line and press enter: ");
buffer =
Console.Read();myPoint.xVal = (
byte)buffer; Console.Write("Enter the End y coordinate of the line and press enter: ");buffer =
Console.Read();myPoint.yVal = (
byte)buffer;myLine.end = myPoint;
Console.WriteLine("\n\n");
Console.WriteLine("Point 1: ({0},{1})", myLine.start.xVal, myLine.start.yVal); Console.WriteLine("Point 2: ({0},{1})", myLine.end.xVal, myLine.end.yVal); Console.WriteLine("Length of line Point 1 to Point 2: {0}", myLine.length()); Console.WriteLine("\n\n");}
Console.Read();}
}
Many thanks in advance

Enter button sensitivity in Console Apps (C# Express)
cbihler
I gave an explanation for that in my initial response
Yep that was me...basically, the Read() command doesn't consume all the bytes in a line, it consumes the line a byte at a time each character you enter on the prompt has an ascii value (from 0-255) that is converted to a byte. The character '1' doesn't equal 1 when read in this manner.
Essentially one response from a user can give you enough bytes to equal several responses (especially if the user puts in a value that is out of range).
The ReadLine() method reads all the input from the prompt and turns it into a string; while the Byte.Parse() method takes that string and attempts to parse it as a byte.
Dave Moran
Thank you to everyone for your help. The program now works fine and I understand why it does thanks to your explainations.
James
giuseppe c
Cockeyed Bob
The issue is that your original code is reading bytes one at a time; the enter key doesn't really have anything to do with it.
If I were to type 123<enter> at the first prompt, I've just put 5 bytes into the input stream (the enter produces two bytes, '\r' and '\n'). The first Console.Read() returns 49 (the ASCII code for '1'), and so myPoint.xVal becomes 49. The second Console.Read() returns 50 ('2'), which is assigned to myPoint.yVal. The third returns 51 ('3'), and the fourth 13 ('\r'). So now all the x and y coordinates have been assigned, the computation proceeds, and the results are written out. The final Console.Read() picks up 10 ('\n'), and the program finishes and terminates.
Since you've essentially provided all the necessary input at the first prompt, the rest of the program probably flashes by too fast to see before the console window closes. The enter key only appears to be the issue because in a console window the bytes aren't actually sent to the program until enter is hit; this makes it possible for the user to edit the input with backspace, etc., before entering it.
As the corrected code shows, you really want to be using ReadLine and the proper parse methods, so that typing 123<enter> at the first prompt actually assigns myPoint.xVal the value 123.
I'm not familiar with the equivalent methods in C++, so I can't offer any guesses as to why it would behave in a different fashion.
-Tom Meschter
Software Dev, Visual C# IDE
Royhwa
buffer = int.Parse(Console.ReadLine());
papaworx
Hi
Thanks very much, that's great.
Don't worry about counter, there were a few lines of code that I cut out and counter was left in there by accident.
One thing I don't understand though, why does the 'enter' key behave so sensitively When I used to do console apps in C++ I never had this problem.
smp
Here is the code that works. A few notes, I have no clue what counter is. Did you even make sure the code compiled before you posted it Second, I'm also not sure what the loop of ten is for, but I left it in there. Your problem was that you were doing a Read() instead of ReadLine(). The difference being that Read() only reads from the console byte by byte (Each character is a byte, and return is two bytes.) ReadLine() takes all the data entered on the console up to (but not including) the enter key.
Also, you should declare buffer as a byte (that way you don't have to recast it everytime). Use the byte.Parse() function to turn the input into a byte (make sure you surround it with a try/catch block for error handling). Finally, use the Math.Pow() function to square a value (see example in provided code.
Hope this helps
using
System;using System.Collections.Generic;
using System.Text;
namespace ReadFromConsole
{
}