home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Delete data after decimal in C++

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1606
Discussions: 200,954, Posts: 2,379,490, Members: 246,326
Old February 21st, 2005, 05:51 PM   Digg it!   #1 (permalink)
Member
 
Join Date: Aug 2004
Posts: 52
Delete data after decimal in C++

I've been sick a bit recently and missed a few days of class, of course right before a lab is due. My issue is I think I need to cut off the remainder decimal after a division problem to successfully finish the project.

"Given an amount, compute the number of quarters, dimes, nickels, and pennies needed. Due to the computer rounding errors you will have to scale your calculations up by 0.005"

Now, to my knowledge rounding isn't in this chapter, nor has it been covered (we've only been in class a few weeks now.)

So I figure, take the amount (let's say .65), divide by .25...get 2.6. If I can cut off the .6, find the sum of quarters, subtract from the total, then divide by dimes and so on, I should be able to finish this.

I just can't figure out how to cut off the data past the decimal and save it as a variable. Or does his comment on scaling imply I am to do it some other way? Any help is greatly appreciated.
dystopia is offline   Reply With Quote
Old February 21st, 2005, 06:16 PM     #2 (permalink)
Anime Otaku
 
RobRich's Avatar
 
Join Date: Oct 2001
Location: Tampa, FL USA
Posts: 108,970
Blog Entries: 15
http://www.codingforums.com/archive/index.php/t-10827
__________________
Robert Richmond | TechIMO Community Relations Director
Infinite perceptions. One reality.
FanFiction.Net - Unleash your imagination.
RobRich is offline   Reply With Quote
Old February 21st, 2005, 06:41 PM     #3 (permalink)
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,552
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
Like in Rob's post, you can either do an explitict cast to an int or you can use the floor function.

Jkrohn
jkrohn is offline   Reply With Quote
Old February 21st, 2005, 06:47 PM     #4 (permalink)
Member
 
Join Date: Aug 2004
Posts: 52
I don't get any compile errors, but I know I'm doing something wrong. It refuses to to change the value... like I said I'm pretty much new to this all :\

These are all pretty well one function programs, so casting to an int I think is out of the question. (I really hope I said that right.)

Code:
// money.cpp 
//
#include <iostream>
using namespace std;

int main()
{
	double amount, quarters, dimes, nickels, pennies,
		afterquar, afterdime, afternick, check;
	double floor(double quarters);
	double floor(double dimes);
	double floor(double nickels);
	cout << "Please enter the amount you wish to find change for. \n\nAmount: ";
    cin >> amount;
	quarters = amount / .25; 
   
	afterquar = amount - quarters * .25; 
	dimes = afterquar / .10;
	
	afterdime = afterquar - dimes * .10;
	nickels = afterdime / .5; 
    
	afternick = afterdime - nickels * .5; 
	pennies = afternick / .01;

    cout << "For " << amount <<" you need the following.\n";
	cout << "\Quarters: " << quarters;
	cout << "\nDimes: " << dimes;
	cout << "\nNickels: " << nickels;
	cout << "\nPennies: " << pennies;

	
	return 0;
}
dystopia is offline   Reply With Quote
Old February 21st, 2005, 06:51 PM     #5 (permalink)
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,552
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
Yikes, you are not understanding

Floor is a function. It takes a avalue and returns one. You don't put it in variable declarations.

Here is a sample.
quarters = floor(amount / .25);
jkrohn is offline   Reply With Quote
Old February 21st, 2005, 06:57 PM     #6 (permalink)
Member
 
Join Date: Aug 2004
Posts: 52
Ah, thank you both, it skipped the nickels, but it didn't say 2.68 quarters again

Edit -- didn't see nickels, because nickels aren't 50 cents a piece lol, I'm so horrible at this stuff.

Last edited by dystopia : February 21st, 2005 at 07:01 PM.
dystopia is offline   Reply With Quote
Old February 22nd, 2005, 02:24 PM     #7 (permalink)
Ultimate Member
 
Join Date: Oct 2003
Location: Aztec, New Mexico
Posts: 1,609
Send a message via AIM to MadMan2k Send a message via MSN to MadMan2k
From my class last semester, we did a similar thing, but it was a longer program:
http://www.techimo.com/forum/t122816.html
__________________
jonbuder.com
MadMan2k is offline   Reply With Quote
Old February 22nd, 2005, 05:02 PM     #8 (permalink)
Member
 
Join Date: Aug 2004
Posts: 52
I guess I forgot to put the finished code. I was pretty suprised how short mine was compared to other peoples in class. Guess it goes to show how everyone codes differently.

Code:
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	double amount, quarters, dimes, nickels, pennies,
		afterquar, afterdime, afternick;

	cout << "Please enter the amount you wish to find change for. \n\nAmount: ";
    cin >> amount;

if (amount < 0 )
{
	cout << "Invalid Amount Entered. \n\n";
	return 0;
}
else
{

	quarters = floor(amount / .25);    
	afterquar = amount - quarters * .25; 
	
	dimes = floor(afterquar / .10);
	afterdime = afterquar - dimes * .10;
	
	nickels = floor(afterdime / .05); 
    afternick = afterdime - nickels * .05; 
	
	pennies = afternick / .01;
    
	cout << "For " << amount <<" you need the following.\n";
	cout << "\nQuarters: " << quarters;
	cout << "\nDimes: " << dimes;
	cout << "\nNickels: " << nickels;
	cout << "\nPennies: " << pennies;
	cout <<"\n\n";

	
	return 0;
}
}
dystopia is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
FIXED worksheet function in MS ACCESS 2000 ctaylor Webmastering and Programming 2 February 17th, 2009 07:52 AM
to quote or not to quote librab103 Webmastering and Programming 7 October 14th, 2003 09:38 AM
how to calculate binary? Creatures Webmastering and Programming 15 March 24th, 2003 03:04 PM
Help! iNeb General Gaming Discussion 23 March 3rd, 2003 06:07 PM
Quick javascript question NineOne Webmastering and Programming 2 November 5th, 2002 12:35 AM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2946)
The disrespect of Obama by Russian .. (41)
Making Health Care Worse (178)
Wireless Televisions. (12)
CPU fan stops spinning randomly (9)
Regular Build (11)
windows 7 problem (7)
Laptop with wireless problem. (5)
Is the PSU I received dead? (12)
Print spooler problem (15)
windows vista security holes (11)
radeon x850xt platinum & shader.. (6)
HIS HD5770 graphic card question (15)
Install XP pro and a Vista laptop ?.. (11)
Recent Discussions
webcam (0)
upgrade for hp a6101 (0)
windows vista security holes (11)
Laptop with wireless problem. (5)
Modern Warfare 2: Who Bought It? (64)
tv not turn on-makes clicking sound (2)
CPU fan stops spinning randomly (9)
EVGA 9800 gtx help with finding a goo.. (11)
Regular Build (11)
Help with onclick and buttons (0)
Virus advise (8)
My monitor won't turn on after instal.. (1)
Internet Lost (3)
Dept. of HS: NSA 'Helped' Develop Vis.. (16)
Point and Shoot Camera Suggestions. (4)
Multiple Restarts Required at Boot (2)
Ideal cheap graph card for PC-Gaming? (18)
radeon x850xt platinum & shader 3 (6)
Graphics Card Upgrade Question (4)
For Sale BFG GTX285 OC2 with 10 year .. (3)
How to convert MP3's (4)
Wireless Televisions. (12)
Hp Artist Edition + Matching Bag (0)
Asus P4G8X Mobo (6)
Xbox 360 GTA: SA disk error (1)


All times are GMT -4. The time now is 06:38 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28