We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,586 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Floating point exception

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.

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
  int num, numer, denom;  // number of fractions, numerator, denominator
  char slash;  // storage for '/' symbol.

  cin >> num;
  cout << "num= " << num << endl;

  for (int i=0; i<num; i++)
    {
      cin >> numer;
      cin >> slash;
      cin >> denom;
      cout << "numer= " << numer << endl;
      cout << "slash= " << slash << endl;
      cout << "denom= " << denom << endl;

////////////////////////////////////
// ERROR HAPPENS AFTER THIS POINT //
////////////////////////////////////

      cout << numer;
      cout << slash;
      cout << denom;

      if (denom = 0)
        cout << " ==> is invalid because denominator is zero";
      if (denom < 0)
        {
          numer = numer * -1;
          denom = abs(denom);
        }

      if (denom > 0)
        cout << " ==> " << numer << "/" << denom;

      if (numer/denom > 0)
        cout << " ==> " << numer/denom << " " << abs(numer%denom) << "/"<< denom;

      cout << endl;

      cin >> numer >> slash >> denom;
    }
  return 0;
}
3
Contributors
7
Replies
1 Hour
Discussion Span
3 Years Ago
Last Updated
9
Views
Question
Answered
Duncans Ghola
Newbie Poster
6 posts since Oct 2009
Reputation Points: 11
Solved Threads: 0
Skill Endorsements: 0

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.

sfuo
Master Poster
713 posts since Jul 2009
Reputation Points: 173
Solved Threads: 111
Skill Endorsements: 0

In your if statement use

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.

Grn Xtrm
Posting Pro in Training
495 posts since Nov 2008
Reputation Points: 100
Solved Threads: 48
Skill Endorsements: 0

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

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.

sfuo
Master Poster
713 posts since Jul 2009
Reputation Points: 173
Solved Threads: 111
Skill Endorsements: 0

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.

Duncans Ghola
Newbie Poster
6 posts since Oct 2009
Reputation Points: 11
Solved Threads: 0
Skill Endorsements: 0

Glad to help. Please mark the thread solved if you have no further questions. Thanks. :)

Grn Xtrm
Posting Pro in Training
495 posts since Nov 2008
Reputation Points: 100
Solved Threads: 48
Skill Endorsements: 0

I knew you would say that because that is number 2 in your list of important things. lol

sfuo
Master Poster
713 posts since Jul 2009
Reputation Points: 173
Solved Threads: 111
Skill Endorsements: 0

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. ;)

Grn Xtrm
Posting Pro in Training
495 posts since Nov 2008
Reputation Points: 100
Solved Threads: 48
Skill Endorsements: 0
Question Answered as of 3 Years Ago by sfuo and Grn Xtrm

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0784 seconds using 2.68MB