1

I'm a beginner programmer and at this moment i try to create primitive program for define variable types. Below code and core of my problem.

namespace Moving
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string input = Console.ReadLine();
            
            if (int.TryParse(input, out int result1))
            {
                Console.WriteLine("type int");
            }
            if (double.TryParse(input, out double result2))
            {
                Console.WriteLine("type double");
            }
            else
            {
                Console.WriteLine("type String");
            }
            Console.ReadLine();
        }
    }
}

When i use string or double it works normally. But when i input int, works first if{} and second if{} too. For example i input 12. Program writes "type int" and "type double" because int may convert to double without efforts. I don't need it. Can i don't convert variable int to double? And i haven't a clue how i can explain to program to see difference between "string" and "char". What may do with it?

Martin Costello
  • 9,672
  • 5
  • 60
  • 72
Aleksandr
  • 11
  • 4
  • try `else if ` instead of just `if`: `else if (double.TryParse(input, out double result2)) {...}` – Dmitry Bychenko Apr 13 '22 at 10:24
  • 7
    That's because a integer is also a double, for example the number `1`, it's obviously an integer, but also a double because (mathematically) `1 = 1.0` is true. You'll want to use `else if` instead of just an `if`. I highly recommend reading the [selection statement](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/selection-statements#the-if-statement) documentation – MindSwipe Apr 13 '22 at 10:24
  • You're going to have to put more logic into how you decide whether the user input is an int or a double in cases where it could reasonably be either – Caius Jard Apr 13 '22 at 10:48

4 Answers4

1

Because (mathematically) 1 = 1.0 is true, knowing this we derive that all integers are also valid decimal numbers, in programming terms we call them floating point numbers, and double is a floating point number type, meaning, it represents a floating point number, and as such can also represent integers*. So technically there's no problem, but it's not behaving how we want it to behave, so how do we fix this?

Easy, we use an else if statement instead of an if statement. The else if is only checked if the if (or else if) before it is false. Implementing it looks like this:

string input = Console.ReadLine();

if (int.TryParse(input, out int intResult))
{
    Console.WriteLine("Integer");
}
else if (double.TryParse(input, out double doubleResult))
{
    Console.WriteLine("Double");
}
else
{
    Console.WriteLine("String");
}

*Okay, so technically double (and every floating point number type) can't represent every integer perfectly, because of how they're implemented in binary, but that's a whole other topic and too advanced for now, if you ever want to know more, check out Is floating point math broken?

MindSwipe
  • 7,193
  • 24
  • 47
  • Thank you MindSwipe. I'll look in post about floating point later. I think it's pretty difficult for me now. I'll definitely read it when I'm done with the basics – Aleksandr Apr 13 '22 at 10:48
  • @Aleksandr no problem, it really is quite an advanced topic to understand, you'll need to understand binary first and also look at how floating point numbers are implemented in binary before really understanding it – MindSwipe Apr 13 '22 at 10:58
0

In your statements, there are two different condition blocks. Therefore, you will get two outputs. If you only want to get one output. You should use one condition block: If, Else If, Else.

        string input = Console.ReadLine();
        
        if (int.TryParse(input, out int result1))
        {
            Console.WriteLine("type int");
        }
        else if (double.TryParse(input, out double result2))
        {
            Console.WriteLine("type double");
        }
        else
        {
            Console.WriteLine("type String");
        }
        Console.ReadLine();
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

Well, you can try else if instead of just if. Please, note, that every int can be parsed as double and every char can be treated as string:

if (int.TryParse(input, out int result1))
    Console.WriteLine("type int");
else if (double.TryParse(input, out double result2))
    Console.WriteLine("type double");
else if (input.Length == 1) // char is a string of Length == 1
    Console.WriteLine("type char");
else
    Console.WriteLine("type String");
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

You can use Convert class to Convert int to double

int n1 = 289;
double n2;
n2 = Convert.ToDouble(n1);
Armin nsr
  • 3
  • 2