Delete data after decimal in C++  | |
February 21st, 2005, 05:51 PM
|
#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. |
| |
February 21st, 2005, 06:16 PM
|
#2 (permalink)
| | Anime Otaku
Join Date: Oct 2001 Location: Tampa, FL USA
Posts: 108,970
|
__________________ Robert Richmond | TechIMO Community Relations Director
Infinite perceptions. One reality. FanFiction.Net - Unleash your imagination. |
| |
February 21st, 2005, 06:41 PM
|
#3 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,552
|
Like in Rob's post, you can either do an explitict cast to an int or you can use the floor function.
Jkrohn |
| |
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;
} |
| |
February 21st, 2005, 06:51 PM
|
#5 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,552
|
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); |
| |
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.
|
| |
February 22nd, 2005, 02:24 PM
|
#7 (permalink)
| | Ultimate Member
Join Date: Oct 2003 Location: Aztec, New Mexico
Posts: 1,609
| |
| |
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;
}
} |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |