c++infinite-loopdo-whiletypeidtypeinfo

Getting infinite loop instead of a value


This is the problem below.

I am getting an infinite loop instead of int type of data in the do-while loop

#include <iostream>
#include <typeinfo>
int main()

{
    int in; // variable in;

    do //loop for check if variable get valid data
    {

        std::cout << "enter : ";

        std::cin >> in;

        if (typeid(in) == typeid(int()))
            break;
        else
            std::cout << "invalid input !! " << std::endl;
    } while (true);
}

Solution

  • The condition

    if (typeid(in) == typeid(int()))
    

    will evaluate to false.

    For it to evaluate to true you will need

    if (typeid(in) == typeid(int))
    

    Running sample

    But, as already said, if what you want is to check for correct inputs, this is not the way to go.