GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Feb-2009, 00:09
Nitsua185 Nitsua185 is offline
New Member
 
Join Date: Feb 2009
Posts: 6
Nitsua185 is on a distinguished road

Floating Point Exception error


Ok 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:
#include <string>
#include <iostream>
using namespace std; 

	const int Quart = 25;
	const int Dime = 10;
	const int Nickel = 5;
	const int penny = 1;

int main() 
{
	int change;
	int NumQuarters;
	int NumDimes;
	int NumNickels;
	int NumPennys;
	
	cout << "Please enter an amount of change between 1 and 99 cents: "; 
	cin >> change; 
	
	NumQuarters = change / Quart;
	NumDimes = change % (NumQuarters * Quart) / Dime ;
	NumNickels = change % (Dime * NumDimes) / Nickel;	
	NumPennys = change % (Nickel * NumNickels) / penny;
	
	
	cout << "Quarters: " << NumQuarters << endl;
	cout << "Dimes: " << NumDimes << endl;
	cout << "Nickels: " << NumNickels << endl;
	cout << "Pennies: " << NumPennys << endl;
	
	return 0;
}
Last edited by LuciWiz : 06-Feb-2009 at 02:24. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 06-Feb-2009, 01:56
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: New C++ student needs some help with very basic code


Most 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:
Originally Posted by Nitsua185
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.
Neither have you disclosed what compiler you are using nor what operating system you are targeting, but most likely you have a floating point operation which is not indicated in the code posted which will require that the floating point library be linked into the executable. You should search through the compiler's documentation to ensure that this library is indeed included when linking.
  #3  
Old 06-Feb-2009, 08:01
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: New C++ student needs some help with very basic code


Your problem is in all of the following statements:
CPP / C++ / C Code:
NumQuarters = change / Quart;
NumDimes = change % (NumQuarters * Quart) / Dime ;
NumNickels = change % (Dime * NumDimes) / Nickel;	
NumPennys = change % (Nickel * NumNickels) / penny;
Notice what happens when NumQuarters is zero. Then, we have:
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:
NumQuarters = 0;
while(change > Quart)
{
  ++NumQuarters;
  change -= Quart;
}

NumDimes = 0;
while(change > Dime)
{
  ++NumDimes;
  change -= Dime;
}

//...etc.

  #4  
Old 06-Feb-2009, 13:36
Nitsua185 Nitsua185 is offline
New Member
 
Join Date: Feb 2009
Posts: 6
Nitsua185 is on a distinguished road

Re: New C++ student needs some help with very basic code


Thanks 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:
#include <string>
#include <iostream>
using namespace std; 

	const int Quart = 25;
	const int Dime = 10;
	const int Nickel = 5;
	const int penny = 1;

int main() 
{
	int change;
	int NumQuarters;
	int NumDimes;
	int NumNickels;
	int NumPennys;
	
	cout << "Please enter an amount of change between 1 and 99 cents: "; 
	cin >> change; 
	
	NumQuarters = 0;
	while(change > Quart)
{
	++NumQuarters;
	change -= Quart;
}
    NumDimes = 0;
	while(change > Dime)
{
	++NumDimes;
    change -= Dime;
}
    NumNickels = 0;
	while(change > Nickel)
{
    ++NumNickels;
    change -= Nickel;
}
    NumPennys = 0;
    while(change > penny)
{
	++NumPennys;
	change -= penny;
}
  

	cout << "Quarters: " << NumQuarters << endl;
	cout << "Dimes: " << NumDimes << endl;
	cout << "Nickels: " << NumNickels << endl;
	cout << "Pennies: " << NumPennys << endl;
	
	return 0;

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  
Old 06-Feb-2009, 13:56
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: New C++ student needs some help with very basic code


I see the problem. Each if() statement should be >= instead of >.
As for the -=:
CPP / C++ / C Code:
// The following lines mean the same thing
a -= 2;
a = a - 2;
  #6  
Old 06-Feb-2009, 14:01
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: New C++ student needs some help with very basic code


Quote:
Originally Posted by Nitsua185
Hehe, im starting to think im not cut out for this stuff, and should change majors immediately.
Nobody understands this stuff immediately. Like anything else, try it for a while and you will grasp more and more of the concepts. Don't be afraid to ask questions. That is what this forum is here for. Good luck.
  #7  
Old 07-Feb-2009, 00:02
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: New C++ student needs some help with very basic code


You 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:
    NumQuarters = change / Quart;
    change -= NumQuarters * Quart;
    NumDimes = change / Dime;
    change -= NumDimes * Dime;
        etc...
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 01:40.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.