0

I come from C, where you can put two scanf with one number in each and there's no problem.

But I'm know starting with C#, and for some reason, when I use Console.Read() more than one time, I can't use the second one. Looks like there's something in the buffer for the second one and doesn't let me add a second number.

Ex:

Console.Write("First number: ");
int input1 = Console.Read();
Console.Write("Second number: ");
int input2 = Console.Read();
Console.Write("{0} + {1} = {2}", input1, input2, input1 + input2);

Using this code, it just let me add a number in the input1, when I press enter to go to the input2, it just skips it and goes to the last sentence.

If I use the number 1 as the first input, it goes to the last sentence saying 49 + 13 = 62.

Does this method works way different than it does in C? Because I don't get it

  • 4
    Did you read the documentation? Read returns a single character code. So 49 is the ASCII code for 1 and then 13 is the character code for carriage return, the next one would be 10 for line feed. The function of Read is explained in the documentation. You presumably want ReadLine. – ProgrammingLlama Jul 19 '22 at 15:19
  • 2
    as The Manual says, [Console.Read()](https://learn.microsoft.com/en-us/dotnet/api/system.console.read?view=net-6.0) gives you a single **character** - which is implicitly converted to its **ASCII**-value when you assign it to an integer. – Franz Gleichmann Jul 19 '22 at 15:19
  • @DiplomacyNotWar isn't ReadLine for strings? Do I have to put yes or yes numbers as strings and then turn them into numbers with Convert32? – Ismael Melero Olea Jul 19 '22 at 15:41
  • 2
    @IsmaelMeleroOlea did you actually _look at the linked duplicate_, which explains in detail what you have to do? – Franz Gleichmann Jul 19 '22 at 15:45
  • @DiplomacyNotWar I totally didn't see it, I thought it was part of my post. I have to get used to the interface. Sorry and thanks for the prompt – Ismael Melero Olea Jul 19 '22 at 16:09

0 Answers0