-2

I am executing the following code, and without introducing a breakpoint I cannot execute the last two lines ., It doesn't execute NumberOut

Console.WriteLine("Enter a positive whole number: ");
            int NumberIn = Console.Read();
            int NumberOut = 0;
            int count = 0;
            while (NumberIn > 0)
            {
                count = count + 1;
                int PartValue = NumberIn % 2;
                NumberIn = NumberIn / 2;
                for (int i = 0; i < count - 1; i++)
                {
                    PartValue = PartValue * 10;
                }
                NumberOut = NumberOut + PartValue;
            }

            Console.WriteLine("the result is: {0}", NumberOut);
            Console.ReadLine();
mjwills
  • 23,389
  • 6
  • 40
  • 63
J Doe.
  • 1
  • 1
  • hi, welcome to SO please read [help], [ask], [tour] to improve your question.. what happens when you step through your code, what do you see? – BugFinder Oct 02 '19 at 13:17
  • @JDoe What are you doing? What is your goal? What do you except for example for 1000? –  Oct 02 '19 at 13:21
  • 2
    You have one problem though, you're using `Console.Read()`, this will not read an *integer* and return that, it will take the first character the user typed, convert that to its encoding value, and return that. For instance, if you type 1000, it will return 49 (49 = the code point for the digit '1'). Could it be that you typed in, say, 4, and expected the sequence 4, 2, 1, 0 and got 52, 26, 13, 6, 3, 1, 0 instead? That it just loops a few more times than expected? – Lasse V. Karlsen Oct 02 '19 at 13:23
  • I entered 5 and got an answer... so.. what exactly are you struggling with – BugFinder Oct 02 '19 at 13:23
  • What **exact** input did you type in? Have you read https://stackoverflow.com/a/28211031/34092 ? – mjwills Oct 02 '19 at 13:23
  • I tested the code, typed in 4, got 110100 as output, which is right for the decimal value 42, which is the code point for the digit 4. This seems to behave exactly like I expect it to, can you tell us how you're verifying that you get no results? – Lasse V. Karlsen Oct 02 '19 at 13:25
  • _It doesn't execute NumberOut_ What does this mean? Is `NumberOut` a function? If you just mean the variable you defined a bit earlier, you cannot _execute_ an int variable. – Joelius Oct 02 '19 at 13:28
  • I tried 5, 22, 12, and many others, any number, and it won't output without a breakpoint. it won't write the answer. I can settle for using a breakpoint but surely there is a way around it without use of breakpoint? @mjwills – J Doe. Oct 02 '19 at 13:28
  • @LasseVågsætherKarlsen I didn't even know this, thank you for the help regarding that. The code simply ends after I enter a number and press enter however, so that was my main issue – J Doe. Oct 02 '19 at 13:30
  • Do you realize that if you do `Console.Read()` will only read one character? Check [the documentation](https://learn.microsoft.com/en-us/dotnet/api/system.console.read?view=netframework-4.7.2) – Cleptus Oct 02 '19 at 13:30
  • Unfortunately the code *should* work, which means there's something else interfering with its execution. What kind of executable project did you make? – Lasse V. Karlsen Oct 02 '19 at 13:30

1 Answers1

0
Console.Read();

Only pulls one character at a time from the input stream, but it also doesn't return until the enter key is pressed. So if you type '1' followed by the enter key, your buffer actually is '1\r\n'. Because you only have one Console.Read only the '1' is pulled off the buffer; therefore when the first Console.ReadLine() is hit the '\r\n' is the pulled off the stream and continues on to your next Console.ReadLine()

Call Console.Read() twice in a row to to remove the enter key off your stream (once for the '\r' and again for the '\n').

still better use console.Readline()

this is a better method since you don't have to worry about processing individual characters on the stream.

Kutlu Ozel
  • 182
  • 1
  • 7