Difference between 2 codes

Rain

New member
Joined
Apr 12, 2020
Messages
2
Programming Experience
Beginner
I was learning to code and I was almost sure that my code is correct. Unfortunately for unknown for me reason I did it wrong. My job was to:
Write a program that reads three numbers from the console and then outputs the middle value of the three numbers. For example:
>54
>4456
>2
54

My code outputs only these 2 orders:
1
2
3 // output = 2
&
2
3
1 // output = 2

but when I'm writing this in this order:
3
2
1

There's no any output.

Here's my code:

C#:
using System;

namespace ConsoleInput
{
    public class TheMiddle
    {
        public static void Main(string[] args)
        {
            string aString = Console.ReadLine();
            string bString = Console.ReadLine();
            string cString = Console.ReadLine();

            int a = int.Parse(aString);
            int b = int.Parse(bString);
            int c = int.Parse(cString);

            int result;
            if (a > b)
            {
                if (c > a)
                    result = a;
                else if (b > c)
                    result = b;
                else
                    result = c;
            }
            else
            {
                if (b < c)
                    result = b;
                else if (a > c)
                    result = a;
                else
                    result = c;

                Console.WriteLine(result);
            }
        }
    }
}

And this is the code which seems to work well:


C#:
using System;

namespace ConsoleInput
{
    public class TheMiddle
    {
        public static void Main(string[] args)
        {
            string aString =  Console.ReadLine();
            string bString =  Console.ReadLine();
            string cString =  Console.ReadLine();

            int a = int.Parse(aString);
            int b = int.Parse(bString);
            int c = int.Parse(cString);
            
            int result;
            if (a < b)
            {
                if (c < a)
                    result = a;
                else if (c > b)
                    result = b;
                else
                    result = c;
            }
            else
            {
                if (c < b)
                    result = b;
                else if (c > a)
                    result = a;
                else
                    result = c;
            }

            Console.WriteLine(result);
        }
    }
}

Could you please tell me what's wrong with my code and what I have to change?
 
You need to debug your code. If you don't know how to debug, now is the time to learn. You're a developer, not just a user, so you need to test code as a developer rather than just as a user. You need to set one or more breakpoints, step through your code line by line and examine the state at each step. You should have a specific expectation of exactly what the code is going to do as each line is executed. As you debug, you can determine whether that expectation is met by examining the state of all appropriate variables and other expressions before and after executing the line of code. As soon as what actually happens differs from your expectations, you have found an issue and you can investigate that specifically. Even if you can't work out how to solve it, at least you can describe the actual issue to use, i.e. where it happens, what data is in use at the time, what you expect to happen and what actually happens. If you get through the entire run without the actual behaviour ever differing from your expectations but the result is still wrong then you know that it is your expectations that are wrong, so you need to go back and re-evaluate them. That means starting again with an algorithm and testing that manually before trying to write any code to implement it. Of course, you didn't write out an algorithm to begin with - who does - so how would you even know if the code does what it's supposed to? You have nothing to compare it to.
 
+1 regarding using a debugger. It'll give you a definite explanation of why things are not working for you. (Small hint: The issue lies in which scope your output printing code lives in.)
 
Back
Top Bottom