【问题标题】:Floating point exception error in simple c++ codeFloating point exception error in simple c++ code
【发布时间】:2022-12-27 15:05:58
【问题描述】:
#include <iostream>
using namespace std;

int main() {
    // your code goes here
    int t,x,y,z,set,i,check=0;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>z;
        
        check=z-(x+y);
       
      for(i=0;i<10;i++){
            if(check%i==0)
            {
                set=i;
            }
           // i++;
        }
        cout<<set<<endl;
    }
        
    return 0;
}

While I run this code then I get run time error like

FLoating point exception

and from next input it gives error like:

dash: 2: 4:not found

【问题讨论】:

  • It is in the check%i line when i is 0. You have a division by zero exception I think.
  • Did you debug your code to see where the exception is being thrown? If you did, then it should be obvious to you as to which line of code is the issue. Just to warn you -- with a rep of 50, not debugging your own code is worthy of a downvote.

标签: c++ c++14


【解决方案1】:

You should not divide i by 0 you should start your loop with 1

【讨论】:

    【解决方案2】:

    There is a floating point exception because I divided i by 0 so I need to start loop from 1.

    【讨论】:

      猜你喜欢
      • 2022-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多