Different value from input
-
i'm new to C# and i'm just doin a test proj... i wonder why this happened?
using System;
namespace test_cs
{
class Program
{
static double calc(double n, double m)
{
return (n * m);
}static void Main(string\[\] args) { Console.WriteLine("Welcome to calculator 1.0"); Console.Write("Enter calculations:"); double num1, num2; char oper; num1 = Convert.ToInt32(Console.Read()); oper = Convert.ToChar(Console.Read()); num2 = Convert.ToInt32(Console.Read()); Console.WriteLine("{0} {1} {2}", num1, oper, num2); Console.ReadLine(); Console.ReadLine(); } }
}
the result is: Imgur: The magic of the Internet[^] anybody can explain why? also, i actually wanna make input streaming like C++
std::cin >> num1 >> oper >> num2;
but haven't know how to do it in C#
-
i'm new to C# and i'm just doin a test proj... i wonder why this happened?
using System;
namespace test_cs
{
class Program
{
static double calc(double n, double m)
{
return (n * m);
}static void Main(string\[\] args) { Console.WriteLine("Welcome to calculator 1.0"); Console.Write("Enter calculations:"); double num1, num2; char oper; num1 = Convert.ToInt32(Console.Read()); oper = Convert.ToChar(Console.Read()); num2 = Convert.ToInt32(Console.Read()); Console.WriteLine("{0} {1} {2}", num1, oper, num2); Console.ReadLine(); Console.ReadLine(); } }
}
the result is: Imgur: The magic of the Internet[^] anybody can explain why? also, i actually wanna make input streaming like C++
std::cin >> num1 >> oper >> num2;
but haven't know how to do it in C#
Simple: COnsole.Read fetches a single character from eth input types by the user, and you are converting that to an integer - but Convert changes type, not value. The character '3' is not the same a a number 3 - it's a character with a particular value within a character set, which is actually 51 in decimal: http://www.asciitable.com/ What I'd suggest is you read it as a line:
string input = Console.ReadLine();
And then "break" that into separate token strings:
Match m = Regex.Match(input, @"^(\\d+)(\[^\\d\])(\\d+)$"); if (m.Success) { int a = int.Parse(m.Groups\[1\].Value); string op = m.Groups\[2\].Value; int b = int.Parse(m.Groups\[3\].Value); Console.WriteLine($"{a} {op} {b}"); }
This also means that it will work if you enter 123+456
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
i'm new to C# and i'm just doin a test proj... i wonder why this happened?
using System;
namespace test_cs
{
class Program
{
static double calc(double n, double m)
{
return (n * m);
}static void Main(string\[\] args) { Console.WriteLine("Welcome to calculator 1.0"); Console.Write("Enter calculations:"); double num1, num2; char oper; num1 = Convert.ToInt32(Console.Read()); oper = Convert.ToChar(Console.Read()); num2 = Convert.ToInt32(Console.Read()); Console.WriteLine("{0} {1} {2}", num1, oper, num2); Console.ReadLine(); Console.ReadLine(); } }
}
the result is: Imgur: The magic of the Internet[^] anybody can explain why? also, i actually wanna make input streaming like C++
std::cin >> num1 >> oper >> num2;
but haven't know how to do it in C#
51 is "3" in the ASCII table, 52 is "4". Console.Read reads a character and returns its ASCII value as an integer.
Returns Int32 The next character from the input stream, or negative one (-1) if there are currently no more characters to be read.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
-
Simple: COnsole.Read fetches a single character from eth input types by the user, and you are converting that to an integer - but Convert changes type, not value. The character '3' is not the same a a number 3 - it's a character with a particular value within a character set, which is actually 51 in decimal: http://www.asciitable.com/ What I'd suggest is you read it as a line:
string input = Console.ReadLine();
And then "break" that into separate token strings:
Match m = Regex.Match(input, @"^(\\d+)(\[^\\d\])(\\d+)$"); if (m.Success) { int a = int.Parse(m.Groups\[1\].Value); string op = m.Groups\[2\].Value; int b = int.Parse(m.Groups\[3\].Value); Console.WriteLine($"{a} {op} {b}"); }
This also means that it will work if you enter 123+456
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
is there any simpler ways like C++? i can typed 9+3 and make input stream like this:
int num1, num2;
char opr;
std::cin >> num1 >> opr >> num2; -
is there any simpler ways like C++? i can typed 9+3 and make input stream like this:
int num1, num2;
char opr;
std::cin >> num1 >> opr >> num2;No. Using the Console to interface with the user is "old fashioned" and you won't be doing it for long anyway. C# was born into a Windows world, and was primarily designed to work in Windows apps. C++ was the child of C, and was born into a world where Windows was new and unpopular, so it was given much more text based user input focus, because it needed it way back then. In the modern world, even C++ apps very rarely use
cin
at all now!"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!