How to write a "if x is a int" type of statement?

Jan 4, 2013 at 8:08pm
I need to do this kind of statement

rough example
if(age == CHAR)
{
printf(" Invalid data type\n");
}
else
{
printf("asodujnao");
}

Basically a type of validation that verifies if user has input a char or int or string.


EDIT
OK i found 'is_numeric' or 'is_string' function but it dosen't properly work
This is the actual program bit

  if(initial == '?')                          //HELP SECTION
    {                                
               system("cls");
               printf("-----Help Section-----\n");
               printf("Enter your first name initial.Eg.Connor would write 'C'\n\n");
               help = 0;            
               }
               if(is_numeric(initial))
               {
                                     printf("trubles");
                                     system("pause");
                                     }


but it comes up with
"is_numeric undeclared, first use this function."
I'm quite a noob at programming so I don't know if it's a library I'm missing or if im writing the is_numeric bit wrong
Last edited on Jan 4, 2013 at 8:40pm
Jan 4, 2013 at 8:15pm
Maybie this:
http://en.cppreference.com/w/cpp/types/is_same

1
2
3
if(std::is_same<decltype(age), char>::value)
{
}
Jan 4, 2013 at 8:27pm
The only time something like this would be useful would be in some kind of template, which I doubt you're asking about.

The user input is going to be whatever format you specify. In this case... whatever type your 'age' variable is, that's what type the user input will be.

1
2
3
4
int age;  // age will ALWAYS be an int.  Regardless of what the user inputs

if( age_is_an_int ) // an if statement like this would be worthless, because
  // it would always be true.  'age' is always going to be an int no matter what. 

Jan 4, 2013 at 8:28pm
How to write a "if x is a int" type of statement

Types are fixed at compile time. If you wrote
int x;
then the type of x is int forever.

validation that verifies if user has input a char or int or string.

A user can only input an int into a variable of type int. Non-numeric chars and strings will be rejected by the I/O code (cin >> or scanf or whatever you're using) - the invalid input will remain unprocessed, at which time you can try reading them into a string (strings accept all input). Or you could read into a string at first, and then examine its contents to see if an int can be made from them.

Last edited on Jan 4, 2013 at 8:29pm
Jan 5, 2013 at 7:54am
closed account (NwvkoG1T)
well why dont you use the isalpha() function in ctype.h? if its not an alphabet (charachter) then it must be an integer?
Jan 5, 2013 at 8:00am
typeof(x) == typeid(int)???
Jan 6, 2013 at 8:15am
morando's answer is quite well, but your compiler shorld support C++ 11, another choice is using Boost::any
Jan 29, 2013 at 8:36am
closed account (NwvkoG1T)
Types are very very disciplined in C++. So if u have defined x as an integer then it will be an integer forever. So I don't understand why you even need this kind o a statement!


If it were a language like python, where any variable can store anything then such a statement would be required and python does have such a statement. as its not required it does not exist!
Topic archived. No new replies allowed.