0

I try to make a multiplier. When I insert number 5, it magically turns to 53. When number 2, it will be 50. Can't see where I made a mistake.

public static void Main(string[] args)
{
    Console.WriteLine("Enter the number: ");
    int num = Console.Read();
    Console.WriteLine();
    int i;

    for (i = 0; i <= 10; i++)
    {
        int result = num * i;
        Console.WriteLine("{0}*{1}={2}", num, i, result);
    }
}
5
  • 2
    Change int num = Console.Read(); to int num = Convert.ToInt32(Console.ReadLine());
    – Equalsk
    Commented Oct 13, 2017 at 8:17
  • 4
    Its ASCII equvalent value of 5 Commented Oct 13, 2017 at 8:17
  • Integer conversion with Console.ReadLine works correctly here: dotnetfiddle.net/UxbT5O. Commented Oct 13, 2017 at 8:19
  • I started with converting but it did not work. So I tried without it.
    – Gulliver
    Commented Oct 13, 2017 at 8:21
  • 1
    When something doesn't work as expected, you should first read the documentation of the function/lib. If you'd do that, you would immediately understand that there is no "magic" here. SO is not a substitute for searching trivial information with a web search engine. Commented Oct 13, 2017 at 8:23

3 Answers 3

7

See the documentation of Console.Read. It returns the next character from the input stream. By assigning it to an int, you convert a char to an int, which yields the character's code.

Use the Console.ReadLine method combined with int.Parse instead:

int num = int.Parse(Console.ReadLine());

This will return the whole input as a string, and int.Parse will attempt to create an int from it.

6
  • 2
    Really, one should be using TryParse as Parse will throw an exception if the user inputs a non-numeric string. Something like if (int.TryParse(Console.ReadLine(), out int num)), then the rest of the code as usual inside and some handling code in the else block. Commented Oct 13, 2017 at 8:32
  • @T.Linnell Definitely, however, I wanted to keep my response as concise as possible, since based on the nature of the question, the OP must be a beginner and therefore I didn't want to add any otherwise unnecessary noise. I assume valid input in this answer.
    – Balázs
    Commented Oct 13, 2017 at 8:34
  • 1
    @T.Linnell, you forgot to add logging and setup project ;)
    – Sinatr
    Commented Oct 13, 2017 at 8:34
  • @Balázs: of course, that's why I upvoted, I just wanted to mention it for completeness as a next step for OP to take. Commented Oct 13, 2017 at 9:15
  • @Sinatr: did I? The scope of OP's code is that it spits out a times-table for a given user input - the code they posted seems to be the entire codebase. I don't think logging would be necessary in a project that small. Commented Oct 13, 2017 at 9:16
1

Use Console.ReadLine() method;

 int num =Convert.ToInt32(Console.ReadLine());
0
 public static void Main(string[] args)
    {
        Console.WriteLine("Enter the number: ");
        string input = Console.ReadLine();
        int num;
        //  Console.WriteLine();
        if (Int32.TryParse(input, out num))
        {
            int i;
            for (i = 0; i <= 10; i++)
            {
                int result = num * i;
                Console.WriteLine("{0}*{1}={2}", num, i, result);
            }
        }
        else
        {
            //not an integer
            Console.WriteLine("not an integer");
        }
        Console.ReadLine();

    }
1
  • Console.ReadLine() takes all inputs from console as string so you need to convert to integer. Int32.TryParse() converts string to equivalent 32 bit signed integer and assigned it to out parameter.Else, you know input is not integer. Thanks Commented Oct 13, 2017 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.