Test for carriage return in playground, console.read() | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Test for carriage return in playground, console.read()

So my mystery character was a carriage return Now I want to test for carriage return so I can skip it. How do I test for carriage return if I have used Console.Read() ? if( (ch != 'N') || (ch != '/r')//Too many characters in character literal if( (ch != 'N') || (ch != char(13)) //Invalid expression term 'char' if( (ch != 'N') || (ch != chr(13)) //The name 'chr' does not exist in the current context https://code.sololearn.com/cbVIGnZyCT97

23rd Sep 2017, 8:33 PM
sneeze
sneeze - avatar
5 Answers
+ 4
A carriage return is denoted as '\r' not '/r'. Are you sure you don't just need to remove the carriage return from the String? string input; input.TrimEnd('\r', '\n'); To get the char for a given ASCII value, use (char) 13
23rd Sep 2017, 8:49 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Found it, it was \n Thanks a lot for your help if( (ch == 'N') || (ch == '\n')) if the character is N -> stop the program if the character is \n (newline) -> skip it else do something nice
23rd Sep 2017, 9:11 PM
sneeze
sneeze - avatar
+ 1
Or if you want to keep using int as the type for input then you can just add: input = Console.Read(); Console.ReadLine(); to catch the remaining newline/carriage return and dispose of it.
23rd Sep 2017, 9:01 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Thanks it works with '/r' this means the are no syntaxt error anymore but is still gives the output "not implemented" so the character read is not a /r (which is 13) I added a Console.WriteLine("input key :" + input); It says this character is input key :10 What character is this or how do I test for this character ? It is in the playground so I can't trim the input string
23rd Sep 2017, 9:07 PM
sneeze
sneeze - avatar
0
Thanks for the help. In the final code I used Console.Readline(). To be able to get the choosen operation and the value's in the same line. https://code.sololearn.com/cbVIGnZyCT97
24th Sep 2017, 8:00 PM
sneeze
sneeze - avatar