Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-24-2007, 09:49 PM   PM User | #1
Derek Zoolander
New Coder

 
Join Date: Apr 2007
Posts: 38
Thanks: 3
Thanked 0 Times in 0 Posts
Derek Zoolander is an unknown quantity at this point
Help with simple C++ functions

I wrote a simple program to determine the least amount of change for a given input, but I need to use a function prototype and incorporate a separate MakeChange function to perform the calculations and return the values.
This is the simple program using one main function:
Code:
#include <iostream>
#include <iomanip>

using namespace std;
int main()
 {
        int amounttochange=1;
	int dollars, cents;
	int quarters, dimes, nickels, pennies;
	
        while(amounttochange!=0)
        {
	        cout << "Enter dollar amount: ";
	        cin >> dollars;
	        cout << "Enter cents: ";
	        cin >> cents;

	        //Do calculations
                amounttochange = (dollars*100)+cents; //convert to cents
	        quarters = amounttochange/25;
	        dimes = (amounttochange%25)/10;
	        nickels = ((amounttochange%25)%(10))/5;
	        pennies = (((amounttochange%25)%(10)%(5)))/1;

	        //Output coins
	        cout << "Quarters: " << quarters <<endl;
	        cout << "Dimes: " << dimes <<endl;
	        cout << "Nickels: " << nickels <<endl;
	        cout << "Pennies: " << pennies <<endl;
	}
	return 0;
}
How can I convert this to use two functions: main and MakeChange? it should take in a value like .86 and return 3 quarters, 1 dime, 1 penny.
amounttochange must be of type double (I don't know how to do double divided by int?). quarters, dimes, nickels, pennies must be of type int.

Will it look something like this?:
Code:
#include <iostream>
#include <iomanip>

using namespace std;

int MakeChange(double amounttochange, int quarters, int dimes, int nickels, int pennies);
int main(?) //do i need any parameters here?
{
        while(double amounttochange!=0)
        {
                cout << "Enter the amount to change (enter 0 to end program): ";
                cin >> amounttochange;

                cout << "Quarters: " << MakeChange(?) <<endl; //? is returned from MakeChange(), right?
                cout << "Dimes: " << MakeChange(?) <<endl;
                cout << "Nickels: " << MakeChange(?) <<endl;
                cout << "Pennies: " << MakeChange(?) <<endl;
        }

        return 0;
}

int MakeChange(double amounttochange, int quarters, int dimes, int nickels, int pennies)
{
        //calculations here
        return ?; //I don't know how to return multiple values for quarters, dimes, etc.
}
Derek Zoolander is offline   Reply With Quote
Old 10-25-2007, 04:37 AM   PM User | #2
ralph l mayo
Regular Coder

 
ralph l mayo's Avatar
 
Join Date: Nov 2005
Posts: 951
Thanks: 1
Thanked 31 Times in 29 Posts
ralph l mayo is on a distinguished road
There are several ways to do this, returning a pointer to an array being among the least preferable. The instructor/text is probably hoping to see you use a struct:

Code:
struct Change
{
	int quarters, dimes, nickles, pennies;
};

Change MakeChange(double amt);
ralph l mayo is offline   Reply With Quote
Old 10-25-2007, 04:51 AM   PM User | #3
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
You could also pass the quarters, dimes, nickel (you could never have more than 1 nickel) and pennies by reference to your MakeChange function.
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

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

Forum Jump


All times are GMT +1. The time now is 03:02 PM.


Advertisement
Log in to turn off these ads.