Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 14th, 2005, 10:45 PM   #1
gardon
Programmer
 
Join Date: Dec 2004
Posts: 87
Rep Power: 4 gardon is on a distinguished road
How do I make this work? simple problem

Ok, I'm trying to make it so you can put in the number of quarters, dimes, nickels, and pennies in that order, and it shows you how many dollars and how many cents that adds up to.

For example:

3 quarters 2 dimes 1 nickel 6 pennies

=

$1.06

And the program should output it like this:

Dollars: 1
Change: .06



Here's my code so far:


#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
cout << "Enter quarters, dimes, nickels, and pennies in that order:\n";
int quarters, dimes, nickels, pennies;
cin >> quarters >> dimes >> nickels >> pennies;

const double penny = .01;
const double nickel = .05;
const double dime = .10;
const double quarter = .25;

double result = (penny * pennies) + (nickel * nickels) + (dime * dimes) + (quarter * quarters);

double change = result % 1;
double dollars = (int)result;
cout << result << " is the total amount" << endl;
cout << "Dollars: " << dollars << endl;
cout << "Change: " << change << endl << endl << endl;

return 0;
}



How do I make it work. I can't figure out what to do with my dollars and cents, and I tried modulousing them result % 1 but I can't because result is double.

Please help, need asap, thanks
Jason
gardon is offline   Reply With Quote
Old Feb 14th, 2005, 10:51 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Well, it would take a little messing around, but it can be done. Printf format specifiers might help.

The way I'd do it would just be to use ints. Have penny as 1, nickel as 5, and so on. When you get the total, the total divided by 100 would be the number of dollars and the total modulus 100 would be cents.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Feb 14th, 2005, 10:51 PM   #3
gardon
Programmer
 
Join Date: Dec 2004
Posts: 87
Rep Power: 4 gardon is on a distinguished road
Woah, I never would have thought of that. Thanks machiavelli


Give me a few moments, and I'll repost my code

Jason
gardon is offline   Reply With Quote
Old Feb 14th, 2005, 10:54 PM   #4
gardon
Programmer
 
Join Date: Dec 2004
Posts: 87
Rep Power: 4 gardon is on a distinguished road
// asdjkfl;.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
cout << "Enter quarters, dimes, nickels, and pennies in that order:\n";
int quarters, dimes, nickels, pennies;
cin >> quarters >> dimes >> nickels >> pennies;

const int penny = 1;
const int nickel = 5;
const int dime = 10;
const int quarter = 25;

int result = (penny * pennies) + (nickel * nickels) + (dime * dimes) + (quarter * quarters);

int change = result % 100;
int dollars = result / 100;
cout << result << " is the total amount" << endl;
cout << "Dollars: " << dollars << endl;
cout << "Change: " << change << endl << endl << endl;

return 0;
}







guess I'm a quick learner

thanks again machiavelli
gardon is offline   Reply With Quote
Old Feb 14th, 2005, 10:56 PM   #5
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
No problem. Glad you got it working.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Feb 15th, 2005, 7:34 AM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Aw... I wanted to answer this one... :p
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:26 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC