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;
}
Grn Xtrm commented: Very nice first post. Thanks! +1

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.

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.

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.

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.

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.