| | |
Floating point exception
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
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.
Any insight into what is going on and how to fix it will be greatly appreciated.
C++ Syntax (Toggle Plain Text)
#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; }
0
#3 Oct 18th, 2009
In your if statement use
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.
C++ Syntax (Toggle Plain Text)
if(denom==0)
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!
URL on facebook!
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
instead of asking the user to put that in that just seems like an unnecessary input that could lead to an input error.
Also I would declare slash right at the start
C++ Syntax (Toggle Plain Text)
char slash = '/';
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!
URL on facebook!
0
#8 Oct 18th, 2009
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!
URL on facebook!
![]() |
Similar Threads
- #8 (floating point exception) (C++)
- Floating Point Exception Using Hash Tables (C++)
- Segmentation Fault becomes floting point exception -- not tied to any specific line (C)
- floating point expression (C++)
- what is "floating point exception" in C++ (C++)
- Floating point exception?? (C++)
- floating point (C++)
Other Threads in the C++ Forum
- Previous Thread: string manipulation
- Next Thread: Problems with strcpy.
| Thread Tools | Search this Thread |
.dll ada api array arrays background based binary bitmap blue business c# c++ calculator char char* class code commentinghelp console data delayload desktop development dns download email embedded encryption engine equation error evc file floatingpoint format fstream function functions game gmail graph gui hash ifstream input int java javac jni linker lnk2019 math method mysql newbie onreadystatechange opengl output partnership php port practice problem program programmer programming projects python read recursion recursive richedit samples screen set snakes space sticky string studio superclass symbian syntax template text transform tree two'scompliment undosend university url variable vista visual visualstudio win32 windows windowsxp xsl






