1) As Salem pointed out, what you are trying to do--even if it did work--would not properly validate the input. The type of a variable does not change, so checking its type does not in any way indicate what type of data the user entered.
2)
I just started programming in C++ but I'm pretty experienced in PHP.
C++ ain't PHP.
Examine the output from this program:
Code:
#include <iostream>
using namespace std;
int main ()
{
//Pointer to an array of characters(an array of characters is known
//as a c-style string):
char str[] = "double";
const char* myPointer = str; //assigns the address of str to myPointer
if(myPointer == "double")
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
//Pointer to a string literal:
const char* myOtherPointer = "double";
if(myOtherPointer == "double")
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
return 0;
}
The reason for the different output has to do with the differences between pointers, character arrays, and how C++ handles string literals(i.e. anything between double quotes), like "double".
In C++, whenever you have a string literal, e.g. "double", on the right side of an equals sign, the compiler takes the string literal, slaps a '\0' character on the end of it and stores it in memory somewhere, and then returns its address. For instance, if you do this:
Code:
const char* myPointer = "double";
then myPointer is assigned the address in memory where the compiler stored the string "double\0". Additionally, the compiler only stores the string literal "double" in memory the first time it is encountered. Subsequently, when the compiler sees "double" again, it just returns the address where it first stored "double" in memory.
Now, depending on what's on the other side of the equals sign opposite the string literal, different things happen. When a char array is on the left side of the equals sign, e.g.
Code:
char str[] = "double";
The compiler goes to the memory location where "double\0" is stored and copies it character by character to a new memory location and names it "str". Then, if you try to compare str to "double" with ==, you are actually comparing the addresses of str and "double\0", and since they are at two different addresses in memory, the addresses aren't equal. To compare the values at those addresses, rather than the addresses themselves, you have to use the strcmp() function.
On the other hand, when you have a pointer variable on the left side of the equals sign opposite the string literal, e.g.
const char* myOtherPointer = "double";
the address of "double\0" is assigned to myOtherPointer. So, if you subsequently compare myOtherPointer to "double" using ==, you will once again compare addresses, but this time they will be equal.
Because of the "erroneous" output you are seeing in your program, it is apparent to me that the name() function must be returning a pointer to a char array containing "double" rather than a pointer directly to the string literal "double".
You will constantly get tripped up by things like that in C++ until you gain a lot more experience.