【问题标题】:why does my calculator program start flashing and scrolling when i enter a large number为什么当我输入一个大数字时我的计算器程序开始闪烁和滚动
【发布时间】:2017-08-12 15:01:51
【问题描述】:

我的程序是一个计算器,目前只做加法和减法,但是当我输入一个大数字时它开始闪烁和滚动。它适用于小数字。该程序不长,所以就在这里。该问题的 youtube 视频https://youtu.be/Fa03WtgXoek

 #include <iostream>

 int GVFU()
{
std::cout <<"enter number";
int a;
std::cin >> a;
return a;
}

int add()
{
int x = GVFU();
int y = GVFU();
int z = x + y;
std::cout <<z <<std::endl;
return 0;
}

int subtract()
{
int x = GVFU();
int y = GVFU();
int z = x - y;
std::cout <<z << std::endl;
return 0;
}

int main()
{
for ( ; ; )
{

std::cout << "enter 1 for addition and 2 for subtraction";
int c;
std::cin >> c;
if (c==1)
{
add();
}
if (c==2)
{
    subtract();
}
std::cout << "press 1 to end";
int e;
std::cin >>e;
if (e==1)
{
    return 0;
}
}
}

【问题讨论】:

  • 您的输入读取功能失败,您没有在任何地方检查错误。
  • 您可能正在接受一个字符来代替整数,从而进入一个无限循环。有关该主题的更多信息in this question

标签: c++


【解决方案1】:

如果您尝试从 cin 读取一个值并且读取的值与预期格式不匹配,则会导致流失败,并且所有未来的读取操作将立即返回而不读取任何内容。

独立地,在 C++ 中,int 类型的整数值具有最小和最大可能值,这取决于您使用的编译器和系统。如果您在输入数字时超过了该值,cin 将认为它读取失败。

综上所述,一旦你输入了一个太大的值,程序将继续运行程序中的主循环,提示输入一个值,在没有实际获取用户输入的情况下立即返回,然后计算垃圾值。

要解决此问题,您需要 (1) 只希望用户不要输入任何意外内容,或者 (2) 让用户输入更加稳健。关于如何在 Stack Overflow 上执行选项 (2) 有很多很好的解释,现在您知道问题的根本原因是什么,您有望修复代码并使其正常工作!

【讨论】:

    【解决方案2】:

    使用

    std::cout << std::numeric_limits<int>::max() << std::endl;
    

    并包含#include &lt;limits&gt;,您会发现您机器上的最大int 值。

    您系统上的

    int 可能是32-bit signed two's complement 数字,这意味着它可以表示的ma​​x 值是2,147,483,647。 如果您添加更大的数字流将失败并且所有下一个读取操作将返回而不读取任何内容。

    使用unsigned long long 可以插入更大的数字。

    【讨论】:

    • 但是,如果用户输入的数字非常大,unsigned long long 不会仍然遇到同样的问题吗?
    • 当然会发生同样的事情,如果他需要插入更大的数字,我只是想给他建议:-)
    【解决方案3】:

    您将输入作为“int”,int 的值范围在 -2,147,483,648 到 2,147,483,647 之间。 这意味着如果您超过此值 2,147,483,647,则不能将其存储为 integer(int) 类型。 对于如此大的数字,您可能应该使用 Long 数据类型。

    【讨论】:

      【解决方案4】:

      如果用户输入超过int limit,您可以在代码中添加以下检查

      int GVFU()
      {
          std::cout <<"enter number";
          int a;
          std::cin >> a;
          if(cin.fail())
          {
              cin.clear();
              cin.ignore(numeric_limits<streamsize>::max(), '\n');
              cout << "Invalid input " << endl;
      
          }
          return a;
      }
      

      如果号码无效,我也会添加退出

      #include <iostream>
      #include <cstdlib>
      
      using namespace std;
      
      int GVFU()
      {
          std::cout <<"enter number";
          int a;
          std::cin >> a;
          if(cin.fail())
          {
              cin.clear();
              cin.ignore(numeric_limits<streamsize>::max(), '\n');
              cout << "Invalid input " << endl;
              std::exit(EXIT_FAILURE);
          }
          return a;
      }
      

      注意:您还可以提供更多信息,而不仅仅是“无效输入” 输出大小或限制信息

      enter 1 for addition and 2 for subtraction1
      enter number4535245242
      Invalid input 
      Program ended with exit code: 1
      

      【讨论】:

      • 更欢迎解释否决票..将有助于修复答案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多