Ben
Ben

Reputation: 75

Simple C# program appears to be skipping code without reason

I had my A-Level AQA Computing exam this morning, and one of the programming questions confused me when my code didn't run lines of code for an unknown reason. I tried recreating it at home to see if it was a one off error for some reason, but it results in the same strange outcome.

The question was asking to write a program using some given pseudocode, this is as close as I can remember it. The program in question was designed to take a two digit number and either; 1) add the two digits, then add the two digits of the result until there was only a one digit number left or 2) Multiply the two digits, and keep doing so until there is only a one digit number left. I can't remember the name of this process but you get the idea. (e.g 77 -> 7+7 = 14, 1+4 = 5. The answer would be 5 in 2 turns/cycles.)

Here is the code;

class Program
{
    static void Main(string[] args)
    {
        int count = 0;
        int value;
        string mode;
        Console.WriteLine("Please enter a value: ");
        value = Console.Read();
        Console.WriteLine("Please enter the mode (a or m): ");
        mode = Console.ReadLine();
        while (value > 9)
        {
            if (mode == "a")
            {
                value = (value / 10) + (value % 10);
            }
            else
            {
                value = (value / 10) * (value % 10);
            }
            count += 1;
        }
        Console.WriteLine("After " + count + " turns, the value is " + value);
    }
}

So it appears to be fine, but when I run it with a test value of 45 for example, this is the output;

Please enter a value:
45
Please enter the mode (a or m):
After 2 turns, the value is 0

This all happens without giving the user chance to enter a mode.

At first it appeared that the code was jumping straight to the while loop after the value was assigned to be bigger than 9. While this confused me I attempted a fix by assigning the users input to a different value (tempvalue), and then assigning that to value after they had selected a mode. However the same happened.

I feel like the solution is going to be something really simple and a stupid mistake that I haven't spotted, but I can't find any errors or possible causes anywhere.

This is driving me insane!! Any help is appreciated.

Thanks :)

DISCLAIMER: This is not me trying to cheat my exam, the exam is over and all work is in so this would not be possible. I am just really curious as to what I had done wrong for this to happen and would be interested to know the solution.

Upvotes: 2

Views: 551

Answers (3)

René Vogt
René Vogt

Reputation: 43886

Your problem is that Console.Read() only reads one character (if available). Console.ReadLine() waits for the Enter key (or \n).

So if you type 45 end hit Enter, 4 is stored in value and 5 is written to mode without further waiting for the user.

So change the Console.Read() to Console.ReadLine(), too. That should solve your problem.

But Console.ReadLine() returns a string. You need to convert this to an int, for example like that:

int value;
string input = Console.ReadLine();
if (!int.TryParse(input, out value) || value < 10 || value > 99)
{
   Console.WriteLine("This was not a two digit number!");
   return;
}

Upvotes: 2

Joey
Joey

Reputation: 354576

You're using Console.Read for the number, instead of ReadLine. Read only reads a single character and not until the next line break (as ReadLine does). So when entering 45↵ what you get returned is just the 4 and the next call to ReadLine will get the 5.

What's more, value will be the Unicode code point of the character 4, which is 52. So you should probably change that line to

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

This then solves both problems (or add error handling to taste, if necessary).

Upvotes: 6

user1023602
user1023602

Reputation:

Console.Read() reads one character '4' and then continues. Console.ReadLine() reads the rest of the line, which is '5'.

So if you change .Read() into .ReadLine() that will help.

Upvotes: 2

Related Questions