Floating point exception

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 3
Reputation: Duncans Ghola is an unknown quantity at this point 
Solved Threads: 0
Duncans Ghola Duncans Ghola is offline Offline
Newbie Poster

Floating point exception

 
1
  #1
Oct 18th, 2009
This program is intended to determine if a fractions is valid (non-zero, non-negative denominator) and to convert an improper fraction into a whole/mixed number. While running the program I get the message "Floating point exception" after it reads the inputs just inside the for loop.

Any insight into what is going on and how to fix it will be greatly appreciated.

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int num, numer, denom; // number of fractions, numerator, denominator
  8. char slash; // storage for '/' symbol.
  9.  
  10. cin >> num;
  11. cout << "num= " << num << endl;
  12.  
  13. for (int i=0; i<num; i++)
  14. {
  15. cin >> numer;
  16. cin >> slash;
  17. cin >> denom;
  18. cout << "numer= " << numer << endl;
  19. cout << "slash= " << slash << endl;
  20. cout << "denom= " << denom << endl;
  21.  
  22. ////////////////////////////////////
  23. // ERROR HAPPENS AFTER THIS POINT //
  24. ////////////////////////////////////
  25.  
  26. cout << numer;
  27. cout << slash;
  28. cout << denom;
  29.  
  30. if (denom = 0)
  31. cout << " ==> is invalid because denominator is zero";
  32. if (denom < 0)
  33. {
  34. numer = numer * -1;
  35. denom = abs(denom);
  36. }
  37.  
  38. if (denom > 0)
  39. cout << " ==> " << numer << "/" << denom;
  40.  
  41. if (numer/denom > 0)
  42. cout << " ==> " << numer/denom << " " << abs(numer%denom) << "/"<< denom;
  43.  
  44. cout << endl;
  45.  
  46. cin >> numer >> slash >> denom;
  47. }
  48. return 0;
  49. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 247
Reputation: sfuo is on a distinguished road 
Solved Threads: 30
sfuo's Avatar
sfuo sfuo is offline Offline
Posting Whiz in Training
 
0
  #2
Oct 18th, 2009
On line 30 you have if( denom = 0 ) and it should be if( denom == 0 ) it crashes because it makes denom equal to zero and it is doing what you are trying to prevent.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 457
Reputation: Grn Xtrm is on a distinguished road 
Solved Threads: 36
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training
 
0
  #3
Oct 18th, 2009
In your if statement use
  1. if(denom==0)
When using if statements you need to use the comparrison operator(==) not the assignment operator (=).
See if that works for you.

EDIT
Looks like sfuo beat me to it by a few seconds. LOL.

I'll also take this oppurtunity to suggest that you include prompts for the user before they enter a number.
Last edited by Grn Xtrm; Oct 18th, 2009 at 6:14 pm.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 247
Reputation: sfuo is on a distinguished road 
Solved Threads: 30
sfuo's Avatar
sfuo sfuo is offline Offline
Posting Whiz in Training
 
0
  #4
Oct 18th, 2009
Yeah like Grn Xtrm's edit says put in prompts before you ask for input because when I ran it the 1st time I didn't know what I was inputting for.

Also I would declare slash right at the start
  1. char slash = '/';
instead of asking the user to put that in that just seems like an unnecessary input that could lead to an input error.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: Duncans Ghola is an unknown quantity at this point 
Solved Threads: 0
Duncans Ghola Duncans Ghola is offline Offline
Newbie Poster
 
0
  #5
Oct 18th, 2009
Ah! Blasted assignment operator. Thanks so much.

As for the char slash, the input is coming from a file and the instructor told us not to bother checking for invalid input. I'm just taking an easy way out.

Thanks again everyone.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 457
Reputation: Grn Xtrm is on a distinguished road 
Solved Threads: 36
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training
 
0
  #6
Oct 18th, 2009
Glad to help. Please mark the thread solved if you have no further questions. Thanks.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 247
Reputation: sfuo is on a distinguished road 
Solved Threads: 30
sfuo's Avatar
sfuo sfuo is offline Offline
Posting Whiz in Training
 
1
  #7
Oct 18th, 2009
I knew you would say that because that is number 2 in your list of important things. lol
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 457
Reputation: Grn Xtrm is on a distinguished road 
Solved Threads: 36
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training
 
0
  #8
Oct 18th, 2009
Originally Posted by sfuo View Post
I knew you would say that because that is number 2 in your list of important things. lol
LOL. Just trying to enforce the rules of the forum.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Reply

Tags
c++, error, floatingpoint

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC