Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Different value from input

Different value from input

Scheduled Pinned Locked Moved C#
csharpc++comtutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chipp_zanuff
    wrote on last edited by
    #1

    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#

    OriginalGriffO L 2 Replies Last reply
    0
    • C chipp_zanuff

      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#

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      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 have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      C 1 Reply Last reply
      0
      • C chipp_zanuff

        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#

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        51 is "3" in the ASCII table, 52 is "4". Console.Read reads a character and returns its ASCII value as an integer.

        MSDN[^] wrote:

        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.

        1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          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!

          C Offline
          C Offline
          chipp_zanuff
          wrote on last edited by
          #4

          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;

          OriginalGriffO 1 Reply Last reply
          0
          • C chipp_zanuff

            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;

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            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!

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups