[英]How can i check a variable type in a conditional statement in c++?
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.我对 c++ 很陌生,当为变量 cont 输入字符串并回答时,我试图让我的程序退出循环时遇到问题。 In python it is pretty easy to do simple checks but I am not sure what I should be doing in cpp.
在 python 中,做简单的检查很容易,但我不确定我应该在 cpp 中做什么。 I tried doing a check using
if(typeid(answer)) == typeid(string))
but this doesnt work.我尝试使用
if(typeid(answer)) == typeid(string))
进行检查,但这不起作用。 I havent tried putting a check for 'y'||'Y'||'n'||'N'
for cont but im assuming it would be something like that?我还没有尝试检查
'y'||'Y'||'n'||'N'
for cont 但我假设它会是这样的吗? just check for those 4 characters?只检查这4个字符?
#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;
}
How can i check a variable type in a conditional statement in c++?
如何在 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.但是,这将始终打印预期的
answer is indeed an int
,因为answer
是int
而不是别的东西。 If the user enters invalid input the variable answer
declared as int
will not turn into a std::string
.如果用户输入无效输入,则声明为
int
的变量answer
不会变成std::string
。
would something like if(inRange(0,200,answer)) work?
if(inRange(0,200,answer)) 之类的东西会起作用吗?
No it would not.不,它不会。
std::cin >> answer;
either succeds to read a number, or it fails and then 0
is assigned to answer
.要么成功读取数字,要么失败,然后将
0
分配给answer
。 You cannot decide if valid input was entered by looking at answer
only.您无法仅通过查看
answer
来确定是否输入了有效输入。
To check if the user entered valid input you can check the state of the stream:要检查用户是否输入了有效输入,您可以检查 stream 的 state:
#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.请注意,这接受例如
42asdf
作为有效输入,因为std::cin >> answer
在遇到不是数字的东西之前确实会读取42
。 For something more sophisticated you can read a std::string
and parse that.对于更复杂的东西,您可以阅读
std::string
并对其进行解析。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.