1

I am pretty new to c++ and im having an issue trying to get my program out of a loop when a string is entered for the variables cont, and answer. In python it is pretty easy to do simple checks but I am not sure what I should be doing in cpp. I tried doing a check using if(typeid(answer)) == typeid(string)) but this doesnt work. I havent tried putting a check for 'y'||'Y'||'n'||'N' for cont but im assuming it would be something like that? just check for those 4 characters?

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

int main() {
    unsigned seed;
    char cont = 'y';
    int answer = 0;
    seed = time(nullptr);
    srand(seed);
    rand() % 100 + 1;


    cout << "Lets play a math game!\n";

     while(cont == 'y')
    {
        int num1 = rand() % 100 + 1;
        int num2 = rand() % 100 + 1;
        cout << "What is the result of this addition? \n " << num1 << '\n' << "+" << num2 << endl;
        cin >> answer;
        if (typeid(answer)==typeid(string))
        {
            while(typeid(answer) == typeid(string))
            {
                cout << "Please enter an integer!" << endl;
                cin >> answer;
            }
        }
        else if (typeid(answer) == typeid(int)) {
            if (answer == (num1 + num2)) {
                cout << "You are correct, would you like to play again?" << endl;
                cin >> cont;
            } else {
                cout << "You were incorrect, would you like to try again? enter y/n" << endl;
                cin >> cont;
            }
        } else {
            answer = 0;
            cout << "You did not enter an integer!\n" << endl;
            cout << "Would you like to try again?" << endl;
        }
    }
    return 0;
}
Michael Rovinsky
  • 6,807
  • 7
  • 15
  • 30
  • 1
    `int answer = 0;` is always an `int`, not a `string`. You can check if a variable is of some type in a `if` but thats not what you actually need – 463035818_is_not_an_ai May 02 '21 at 16:58
  • would something like if(inRange(0,200,answer)) work? What happens when a character is entered? do characters hold some integer value – Jonathan Tyler Ecton-Rodriguez May 02 '21 at 17:11
  • Unrelated: You should read [Why is the use of rand() considered bad?](https://stackoverflow.com/questions/52869166/why-is-the-use-of-rand-considered-bad) –  May 02 '21 at 17:13
  • 1
    In Python you read a string and convert it to a number, e.g. `int(input())`. In C++ this is done in one step with `int answer; cin >> answer;`. You have to check the state of `cin` to see if the read and conversion were successful. If it fails an error bit is set. See https://en.cppreference.com/w/cpp/io/ios_base/iostate. Or you can do the same as in Python. Read it into a string and convert it: `std::string answer; cin >> answer; std::stoi(answer);` –  May 02 '21 at 17:17

1 Answers1

0

How can i check a variable type in a conditional statement in c++?

You do that already, though I'd do this instead:

#include <type_traits>
#include <iostream>
int main() {
    int answer =0;
    if constexpr(std::is_same_v<int,decltype(answer)>) {
        std::cout << "answer is indeed an int";
    }
}

However, this will always print the expected answer is indeed an int, because answer is an int not something else. If the user enters invalid input the variable answer declared as int will not turn into a std::string.

would something like if(inRange(0,200,answer)) work?

No it would not. std::cin >> answer; either succeds to read a number, or it fails and then 0 is assigned to answer. You cannot decide if valid input was entered by looking at answer only.

To check if the user entered valid input you can check the state of the stream:

#include <iostream>
#include <limits>

int main() {
    int answer =0;

    while(!(std::cin >> answer)){
        std::cout <<  "please enter a number\n";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    std::cout << answer;
}

Note that this accepts for example 42asdf as valid input, because std::cin >> answer does read 42 before it encounters something that is not a number. For something more sophisticated you can read a std::string and parse that.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • So, this works great, and I do not get stuck in any loops anymore, but for some reason I have to input answer twice. it does not react to the first answer, and it will prompt correct, incorrect, or please enter a number on the 2nd answer attempt. Im trying to copy paste my code into this comment but the formatting is completely off and confusing. ``` cin >> answer; while(!(cin >> answer)) { cout << "please enter a number\n"; cin.clear(); cin.ignore(numeric_limits::max(), '\n'); } if (answer == (num1 + num2)) ``` – Jonathan Tyler Ecton-Rodriguez May 02 '21 at 19:24
  • @JonathanTylerEcton-Rodriguez don't call `std::cin >> answer` twice. `while(!(std::cin >> answer))` does read `answer` and then checks the state of the stream – 463035818_is_not_an_ai May 02 '21 at 20:11
  • thanks, you were a great help, i learned a lot from this :) – Jonathan Tyler Ecton-Rodriguez May 03 '21 at 01:43