I was given this assignment to create a function and have the user input a number and the function is suppose to give you the amount of change back. My program is listed below

#include<iostream>
using namespace std;
int change(int,int& ,int& ,int& ,int& ,int& ,int& );

int main()
{
	int a; 
	int b;
	int c,d,e,f,g,h;
	cout<<"Please enter a dollar amount: $";
	cin>>b;
	a=change(b,c,d,e,f,g,h);
cin.get();
return 0;
}

int change(int x, int& hundreds,int& fifties,int& twenties,int& tens,int& fives,int& ones)
{	

	hundreds=x/100;
		cout<<hundreds<<endl;
	fifties=x/50;
		cout<<fifties<<endl;
	twenties=x/20;
		cout<<twenties<<endl;
	tens=x/10;
		cout<<tens<<endl;
	fives=x/5;
		cout<<fives<<endl;
	ones=x/1;
		cout<<ones<<endl;
cin.get();
return 0;
}

If I enter $126 it gives me it gives me the maximum amount of what a hundred or fifty dollar bill can go into it. But the direction says that the function is to consider the integer passed value as a dollar amount and convert the value into the least number of equivalent bills. Using the references, the function should directly alter the respective arguments in the calling function.

I know my program is wrong but I'm not completely understanding the whole reference variables and I'm just completely stuck on how to do it correctly.

Firstly the reference variables thing. In the context of parameter passing to functions, using a reference variable has the effect that any change to the variable inside the function will also change the variable in the caller.
So, in this simple example, x is passed by reference and is altered by the call. y is passed by value and does not change:

{
    int x = 0;
    int y = 0;
    doSomething(x, y);

    // x is now 1
    // y is still 0
}
void doSomething(int& p1, int p2)
{
    p1 = 1;
    p2 = 1;
}

You do seem to have passed your variables by reference correctly, so you could calculate the change inside the function, then print out the results (c,d,e,f,g,h) in the main part, BUT,
you do need to revisit your calculation of the change. Having determined that, for example, $126 contains one $100 bill, you should remove that $100 from the total before continuing with $50 etc.

Firstly the reference variables thing. In the context of parameter passing to functions, using a reference variable has the effect that any change to the variable inside the function will also change the variable in the caller.
So, in this simple example, x is passed by reference and is altered by the call. y is passed by value and does not change:

{
    int x = 0;
    int y = 0;
    doSomething(x, y);

    // x is now 1
    // y is still 0
}
void doSomething(int& p1, int p2)
{
    p1 = 1;
    p2 = 1;
}

You do seem to have passed your variables by reference correctly, so you could calculate the change inside the function, then print out the results (c,d,e,f,g,h) in the main part, BUT,
you do need to revisit your calculation of the change. Having determined that, for example, $126 contains one $100 bill, you should remove that $100 from the total before continuing with $50 etc.

Thanks for your reply to my thread it helped out a lot and I was able to complete my assignment thanks a mil!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.