[Solved] Can’t compare types of variables in C++ [closed]

Introduction

C++ is a powerful programming language that allows developers to create complex applications. One of the most important aspects of C++ is its ability to compare different types of variables. However, it is important to note that C++ does not allow for the comparison of different types of variables. This means that if you are trying to compare two variables of different types, such as an integer and a string, the comparison will not work. In this article, we will discuss why C++ does not allow for the comparison of different types of variables and what alternatives are available.

Solution

No, you cannot compare types of variables in C++. The language does not provide any built-in functionality for this purpose. However, you can use typeid() to get the type information of a variable, and then use that information to compare the types.


typeid(real) == typeid(double) will always be true since real is a double.

If you want to check that you have valid input, you need to actually check the input. Here is one way of doing that

double inputReal()
{
    std::size_t end_pos{};
    std::string input;
    std::cout << "Enter real part: ";
    while (getline(std::cin, input))              // get the full line of input
    {
        double real = std::stod(input, &end_pos); // convert to double 
        if (end_pos == input.size())              // check to see if all of the input was converted
            return real;
        else
            std::cout << "Incorrect input. Try another input: ";
    }
}

1

When it comes to comparing variables in C++, it can be a tricky process. Different types of variables have different rules for comparison, and it is important to understand these rules in order to make sure that your code is working correctly. Unfortunately, it is not possible to compare variables of different types in C++. This means that if you are trying to compare two variables, they must both be of the same type.

For example, if you are trying to compare two integers, you can use the comparison operators such as <, >, <=, and >=. However, if you are trying to compare an integer and a string, this is not possible. The same is true for comparing two strings, or two floating-point numbers. In each case, the variables must be of the same type in order to be compared.

It is also important to note that some types of variables cannot be compared at all. For example, pointers cannot be compared in C++. This means that if you are trying to compare two pointers, you will not be able to do so. Similarly, objects cannot be compared in C++, so if you are trying to compare two objects, this is not possible either.

In summary, it is not possible to compare variables of different types in C++. If you are trying to compare two variables, they must both be of the same type. Additionally, some types of variables cannot be compared at all, such as pointers and objects. Understanding these rules is important for making sure that your code is working correctly.