Question I need help with a code

Junes

New member
Joined
Feb 17, 2020
Messages
2
Programming Experience
Beginner
Hi,
So i need help with my code.
So i want that the user writes a number and then another number. then with an if i want to check which one is bigger.
But that isnt working.
Heres my code:

C#:
int result;

Console.Write("Write a number: ");

string num1 = Console.ReadLine();

Console.Write("Write another number: ");

string num2 = Console.ReadLine();

if (num1 > num2)
{
    result = num1;
}
else
{
    result = num2;
}

return result;

Console.ReadLine();
 
Last edited by a moderator:
Welcome to the forums.

Please put your code in code tags

nD918n6.gif


But that isnt working.
Isn't working how?

What result are you expecting?

What are the values of num1 and num2, more importantly what types are they?

Why are you not converting your string to int, double etc?

Why are you not parsing the inputted value and testing to see what type num1 and num2 are?

 
Here is what I want you to do. Take this and test with it, so you can see how the different values are interpreted. Following this link : Navigate code with the debugger - Visual Studio You can learn how to debug this code. In the file with your code, place a break point on the lines of interest, just like this :

Screenshot_71.jpg


And step through the debugging process by pressing F11 once the break point has been hit to move to the next step in the debugging process. Once a break point has been reached, you can hover your mouse over your area of interest and inspect all of the details of that object. Just as I am doing in this screenshot below. You can see that the object is null, because it hasn't been initialised yet :

Screenshot_72.jpg


Not; only when i press F11 and pass the section where the thread becomes instantiated, can i then access all of the specs of this object which allows me to inquire into all of the properties etc :

Screenshot_73.jpg


The name property is null because it hasn't been hit yet in the above screenshot.

Screenshot_74.jpg


The same applies for when i set a property of an object, just as the name of the object was null before the property value was set. This is how we read the code using the debugger and you need to learn to use it so you too can debug your own code for inconsistencies and irregularities in your code, and this will greatly help you to identify problems in your code. You should read the link I linked above, as I am only touching on the mild stuff and debugging can get quite deep especially when you start executing commands in the debugging window to compute values or expressions and so much more.

The below code is something for you to play with, and so you can understand how the code below behaves....

If you initialise some default values :
C#:
int result = 0, num1 = 0, num2 = 0; bool bool_IsGreater;
Then get the console to ask the user for input :
C#:
            Console.Write("Write a number: ");
            string str1 = Console.ReadLine();
Then check the users input to see if it is actually numeric :
C#:
if (str1.All(char.IsDigit) && int.TryParse(str1, out num1))
            {
                Console.Write("Write another number: ");
            }
Some might think that the int.TryParse is redundant because we are already checking if numeric with our first Linq condition. But its not redundant, because should the parse happen, we want to update num1 with the user input by parsing. Else :
C#:
            else
                Console.WriteLine("The number you wrote is not numeric. Try again...");
Then we perform the second step for str2 and num2 just as we done above :
C#:
string str2 = Console.ReadLine();
            if (str2.All(char.IsDigit) && int.TryParse(str2, out num2))
            {
                bool_IsGreater = num1 > num2;
            }
Except we take a Boolean value to indicate the condition of num1 being greater than num2. If its true, we have a larger number, and false if we don't. Else :
C#:
else
                Console.WriteLine("The number you wrote is not numeric. Try again...");
Additionally you can also just check if the input is numeric, and then convert the strings to integers. Then you can work directly with your integral values which was converted from user input :
C#:
            if (str1.All(char.IsDigit) && str2.All(char.IsDigit))
            {
                num1 = Convert.ToInt32(str1); num2 = Convert.ToInt32(str2);
                if (num1 > num2)
                {
                    result = num1;
                    Console.WriteLine($"{result} is greater than {num2}");
                }
                else
                {
                    result = num2;
                    Console.WriteLine($"{num1} is less than {result}");
                }
            }
You should note however, that if your user input is 2.1 or consists of a point or comma in-between any numbers, it will fail to pass as a valid condition of the first part of the if statement based on the Linq condition of str1.All(char.IsDigit).
In which case, you would need to tweak your variables like so :
C#:
            double result = 0; double num1 = 0, num2 = 0;
            bool bool_IsGreater;
            Console.Write("Write a number: ");
            string str1 = Console.ReadLine();
            if (double.TryParse(str1, out num1))
            {
                Console.Write("Write another number: ");
            }
            else
                Console.WriteLine("The number you wrote is not numeric. Try again...");
            string str2 = Console.ReadLine();
            if (double.TryParse(str2, out num2))
            {
                bool_IsGreater = num1 > num2;
            }
            else
                Console.WriteLine("The number you wrote is not numeric. Try again...");
 

Latest posts

Back
Top Bottom