3

Hi i'm having trouble making a function that checks the data type of a variable and checks it to make sure if a data type is similar to it in C++. Here's my code so far:

#include <iostream>
#include <typeinfo>
using namespace std;




int main() {

    int typeCheck(string words, string typeWanted);

    //make number assurance function        .

        string word;
        cin >> word;

        typeCheck(word, "string");

}

int typeCheck(string words, string typeWanted) {
    if (typeid(words).name() != typeid(typeWanted).name()) {
        cin.clear();
        cin.ignore();
        return 0;
    }
    else if (typeid(words).name()== typeid(typeWanted).name())
        cout << "All good";
}

When I run the code it keeps saying the same output which is: All good even if I put a string or an int when its not the correct one. Instead of saying this I want it to clear the buffer and ignore it. Can anyone help me with this problem? Thanks in advance!

liil
  • 37
  • 4
  • 1
    I don't understand why you are using `typeid`. It can be as simple as `words == typeWanted`. – R Sahu Oct 12 '19 at 21:39
  • 1
    i think you are misunderstanding what typeid is for. it returns the type - but `words` and `typeWanted` are both declared as strings. – AndersK Oct 12 '19 at 21:41
  • Could it be that you enter a typename as a string and want to check it against a variable? Btw use `else` instead of `else if` – Dimfred Oct 12 '19 at 21:42

2 Answers2

3

C++ is a statically typed language, meaning that the type is known at compile time. It will never be known only at run time.

What that means is that in your example:

int typeCheck(string words, string typeWanted);

both words and typeWanted will always be strings. If it is ever not a string, it will fail to compile. Thus, using typeid() in this situation is somewhat pointless. Your if statement will always be false, and your else-if statement will always be true.

Instead, you would want to use typeid() when you don't know they will be the same type, like in some sort of template situation:

template <class WordsType, class TypeWantedType>
int typeCheck(WordsType words, TypeWantedType typeWanted);

Here, a typeid() comparison makes more sense, because you don't really know if words and typeWanted are both strings.

You could do something like this instead:

template <class WordsType>
int typeCheck(WordsType words, string typeWanted) {
    if (typeid(words).name() != typeWanted) {
        //...
    }
    // ...
}

This would compare an unknown type to a wanted type.

Finally, there is another option, @RSahu's option from the comments:

if(words == typeWanted) {
//...
}

This will compare the input the user gave to the string "string".

It's unclear to me what you want to do exactly, so I can't recommend which one you should use, but at least now you have a couple of options.

  • You're welcome. If this solved your problem, you can always accept the answer. If this doesn't, then you can always edit your question and explain why this doesn't help, or you can accept another answer that did solve your problem. –  Oct 12 '19 at 22:21
2

It is because you are converting the type to string eaither way so it will allways trigger as correct as long as it is a string in the function input.

I would recommend that you use a template type so that whatever you enter into the TypeCheck() function will retain its type.

UnReal G
  • 106
  • 2
  • 8