无法比较 C++ 中的变量类型

huangapple go评论92阅读模式
英文:

Can't compare types of variables in C++

问题

  1. 我有以下在一个类中的函数,它获取一个复数的实部:
  2. ```cpp
  3. double inputReal(){
  4. double real;
  5. cout << "Enter real part: ";
  6. cin >> real;
  7. bool compare = typeid(real) == typeid(double);
  8. while (compare != true){
  9. cout << "Incorrect input. Try another input: ";
  10. cin >> real;
  11. }
  12. return real;
  13. }

它应该检查实部是否实际上是一个数字,如果实部不是一个数字 - 它应该获取另一个输入。然而,如果我尝试输入类似于:"aa" 或 "#@",它会跳过 while 部分并继续输出,忽略获取虚部:

  1. double inputImag(){
  2. double imaginary;
  3. cout << "Enter imaginary part: ";
  4. cin >> imaginary;
  5. bool compare = typeid(imaginary) == typeid(double);
  6. while (compare != true){
  7. cout << "Incorrect input. Try another input: ";
  8. cin >> imaginary;
  9. }
  10. return imaginary;
  11. }

如果我尝试将 "aa" 输入为实部,输出如下:

  1. Enter real part: aa
  2. Enter imaginary part: 复数的绝对值: 0
  3. 复数的幅角: 0
  4. 程序以退出代码结束: 0

如果我正确输入,代码运行得很好,所以我怀疑是这些函数内部类型比较出了问题。我需要知道我做错了什么以及如何修复它。我对 C++ 相对较新,我的大学教授要求检查用户输入是否正确。如果问题看起来不清楚,请随时询问。

  1. <details>
  2. <summary>英文:</summary>
  3. I have following function within a class, which gets real part of a complex number:

double inputReal(){
double real;
cout << "Enter real part: ";
cin >> real;
bool compare = typeid(real) == typeid(double);
while (compare != true){
cout << "Incorrect input. Try another input: ";
cin >> real;
}
return real;
}

  1. It should check if real is *actually a number* and if real isn&#39;t a number - it should get another input. However, if I try inputing something like: &quot;aa&quot; or &quot;#@&quot;, it skips the *while* part and proceeds to output, ignoring getting the imaginary part:

double inputImag(){
double imaginary;
cout << "Enter imaginary part: ";
cin >> imaginary;
bool compare = typeid(imaginary) == typeid(double);
while (compare != true){
cout << "Incorrect input. Try another input: ";
cin >> imaginary;
}
return imaginary;
}

  1. Output, if I try inputing &quot;aa&quot; as a real part:

Enter real part: aa
Enter imaginary part: Absolute value of complex number: 0
Argument of complex value: 0
Program ended with exit code: 0

  1. The code works just fine, if I input correctly, so I suspect it&#39;s the comparison of types within those functions. I need to know what did I do wrong and how I can fix it. I&#39;m relatively new to C++ and my college professor requires checking, that user inputs correctly. Feel free to ask anything, if the question seems unclear.
  2. </details>
  3. # 答案1
  4. **得分**: 0
  5. ```cpp
  6. `typeid(real) == typeid(double)` 将始终为true,因为`real`是一个`double`。
  7. 如果您想检查您是否有有效的输入,您需要实际检查输入。以下是一种方法:

double inputReal()
{
std::size_t end_pos{};
std::string input;
std::cout << "输入实部:";
while (getline(std::cin, input)) // 获取完整的输入行
{
double real = std::stod(input, &end_pos); // 转换为double
if (end_pos == input.size()) // 检查是否所有输入都被转换
return real;
else
std::cout << "输入不正确。请尝试另一个输入:";
}
}

  1. <details>
  2. <summary>英文:</summary>
  3. `typeid(real) == typeid(double)` will always be true since `real` is a `double`.
  4. If you want to check that you have valid input, you need to actually check the input. Here is one way of doing that

double inputReal()
{
std::size_t end_pos{};
std::string input;
std::cout << "Enter real part: ";
while (getline(std::cin, input)) // get the full line of input
{
double real = std::stod(input, &end_pos); // convert to double
if (end_pos == input.size()) // check to see if all of the input was converted
return real;
else
std::cout << "Incorrect input. Try another input: ";
}
}

huangapple
  • 本文由 发表于 2023年4月6日 20:27:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定