![]() |
|
#1
|
|||
|
|||
Floating Point Exception errorOk so i have an assignment for a class to write a program that computes the change(in coins) given for anything from 1 to 99 cents. So far here is the code ive put together in about 5 minutes just to check my formulas. Its very incomplete i know but im just trying to get the math part right. When i compile this program, run it, and enter an integer it gives me a "Floating Point Exception" error. Can someone PLEAAASE help me with this.
CPP / C++ / C Code:
Last edited by LuciWiz : 06-Feb-2009 at 02:24.
Reason: Please insert your C++ code between [cpp] & [/cpp] tags
|
|||
|
#2
|
|||
|
|||
Re: New C++ student needs some help with very basic codeMost likely, one of the Moderators will rightfully deduct reputation points for not enclosing your code with [cpp] before & [/cpp] afterwards which is pointed out in the posting guidelines:
http://www.gidforums.com/t-5566.html You may find this picky, but we are inundated on a daily basis by those with threads entitled "Help me!" to "I don't understand this!" wanting us to spend time figuring out what has been done wrong. The least you can do is make your request presentable. Quote:
|
|
#3
|
|||
|
|||
Re: New C++ student needs some help with very basic codeYour problem is in all of the following statements:
CPP / C++ / C Code:
NumDimes = change % (0)/Dime....which simplifies to NumDimes = change % 0....this is the same as dividing by zero. It is not allowed. Here is a simple way to do this: CPP / C++ / C Code:
|
|
#4
|
|||
|
|||
Re: New C++ student needs some help with very basic codeThanks for the help big time FakePoo, that fixed my floating point problem. Now Its giving me a return value for each coin, it gets very close to getting the original amount right buts its two or three off everytime. Sorry to be bothersome, but if you have the time could you explain to me what the following statement means. Specifically the - before the = sign, I havent learned these types of statements yet in my class.
NumQuarters = 0; while(change > Quart) ++NumQuarters; change -= Quart; } Once I understand exactly what that statement means i should be able to fix whatever is happening in my program to give me the wrong value. Also heres my new code: CPP / C++ / C Code:
and here are a few runs of the program: Please enter an amount of change between 1 and 99 cents: 75 Quarters: 2 Dimes: 2 Nickels: 0 Pennies: 4 710 ranger2$ a.out Please enter an amount of change between 1 and 99 cents: 20 Quarters: 0 Dimes: 1 Nickels: 1 Pennies: 4 711 ranger2$ a.out Please enter an amount of change between 1 and 99 cents: 54 Quarters: 2 Dimes: 0 Nickels: 0 Pennies: 3 Hehe, im starting to think im not cut out for this stuff, and should change majors immediately. |
|
#5
|
|||
|
|||
Re: New C++ student needs some help with very basic codeI see the problem. Each if() statement should be >= instead of >.
As for the -=: CPP / C++ / C Code:
|
|
#6
|
|||
|
|||
Re: New C++ student needs some help with very basic codeQuote:
|
|
#7
|
||||
|
||||
Re: New C++ student needs some help with very basic codeYou were close with your first program. After figuring out the number of quarters, remove them from change and you won't need any loops at all:
CPP / C++ / C Code:
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
Recent GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to sort random access file? | wmmccoy0910 | C Programming Language | 12 | 04-Sep-2006 04:40 |
| Here it is again! 35% - 40% off For Life! | my-e-space | Web Hosting Advertisements & Offers | 0 | 20-Apr-2006 15:48 |
| C Code i need help for printing | batman3280 | C Programming Language | 8 | 13-Mar-2006 15:48 |
| Help in C Print is not working with LinkList | batman3280 | C Programming Language | 3 | 09-Mar-2006 20:03 |
| User defined headers | davis | Miscellaneous Programming Forum | 6 | 16-Feb-2006 19:40 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The