954,206 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Assistance with change program

i have this simple program im writing to help my dads business.

thoguh it doesnt seem to be working, this is what i have currently

#include<iostream>
using namespace std;

int change(double money,int onedollar,int fiftycent,int twentycent,int tencent,int fivecent);

int main()
{
	int money, onedollar, fiftycent, twentycent, tencent, fivecent;
	
	cout << "Enter amount: ";
	cin >> money;
	
	change(money, onedollar, fiftycent, twentycent, tencent, fivecent);
	
	cout << "One Dollar: " << onedollar << endl;
	cout << "Fifty Cent: " << fiftycent << endl;
	cout << "Twenty Cent: " << twentycent << endl;
	cout << "Ten Cent: " << tencent << endl;
	cout << "Five Cent: " << fivecent << endl;
	return 0;
	
}

int change(float money, int onedollar, int fiftycent, int twentycent, int tencent, int fivecent)

{
	float temp;
	
	temp = money;
	
	temp *= 100;
	onedollar = temp/100;
	temp = temp%100;
	fiftycent = temp/50;
	temp = temp%50;
	twentycent = temp/20;
	temp = temp%20;
	tencent = temp/10;
	temp = temp%10;
	fivecent = temp/5;
	
	return 0;
	
}

i dont know if ive got the right idea or anything

this is sorta what im trying to write, a C++ function, named change(), that has a floating-point parameter and 5 integer reference parameters named onedollars, fiftycents, twentycents, tencents and fivecents. The function is to consider the floating-point passed value as a dollar amount and convert the value into an equivalent number of dollar coins, fifty, twenty, ten and five cents coins. Using the references, the function should directly alter the respective arguments in the calling function.

For example, calling the function with the following arguments: change(78.85, onedollars, fiftycents, twentycents, tencents, fivecents); will assign the following values to the appropriate variables:
onedollars = 78
fiftycents = 1
twentycents = 1
tencents = 1
fivecents = 1


any help wuld b awsome

Slavrix
Junior Poster in Training
70 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

what you need is integer division. in your program, temp is a float. float(123)/100 will give you 1.23, not 1. and a standards-compliant compiler would give you a compile-time error if you try float(123)%100

vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

so something like

int temp

temp = money * 100


?

Slavrix
Junior Poster in Training
70 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

so something like

int temp

temp = money * 100
?


yes. and if you want the function to have output parameters, pass them by reference, not value.
also passing the amount in cents (as an integer) rather than a float would help avoid floating point round off errors.

vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

You should have a function called change so that you can intiate it once and call it whenever you need it!

i have this simple program im writing to help my dads business.

thoguh it doesnt seem to be working, this is what i have currently

#include<iostream>
using namespace std;

int change(double money,int onedollar,int fiftycent,int twentycent,int tencent,int fivecent);

int main()
{
    int money, onedollar, fiftycent, twentycent, tencent, fivecent;
    
    cout << "Enter amount: ";
    cin >> money;
    
    change(money, onedollar, fiftycent, twentycent, tencent, fivecent);
    
    cout << "One Dollar: " << onedollar << endl;
    cout << "Fifty Cent: " << fiftycent << endl;
    cout << "Twenty Cent: " << twentycent << endl;
    cout << "Ten Cent: " << tencent << endl;
    cout << "Five Cent: " << fivecent << endl;
    return 0;
    
}

int change(float money, int onedollar, int fiftycent, int twentycent, int tencent, int fivecent)

{
    float temp;
    
    temp = money;
    
    temp *= 100;
    onedollar = temp/100;
    temp = temp%100;
    fiftycent = temp/50;
    temp = temp%50;
    twentycent = temp/20;
    temp = temp%20;
    tencent = temp/10;
    temp = temp%10;
    fivecent = temp/5;
    
    return 0;
    
}

i dont know if ive got the right idea or anything

this is sorta what im trying to write, a C++ function, named change(), that has a floating-point parameter and 5 integer reference parameters named onedollars, fiftycents, twentycents, tencents and fivecents. The function is to consider the floating-point passed value as a dollar amount and convert the value into an equivalent number of dollar coins, fifty, twenty, ten and five cents coins. Using the references, the function should directly alter the respective arguments in the calling function.

For example, calling the function with the following arguments: change(78.85, onedollars, fiftycents, twentycents, tencents, fivecents); will assign the following values to the appropriate variables: onedollars = 78 fiftycents = 1 twentycents = 1 tencents = 1 fivecents = 1

any help wuld b awsome

~Ken Esquire~
Newbie Poster
3 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 
i have this simple program im writing to help my dads business.


Yeah, right. Look, we don't mind helping people with their homework assignments. So you don't have to lie about it in a lame attempt to get homework help.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You