I am a student with an assignment to wirite a program that makes change but i am not getting the correct output what am i doing wrong?

#include <iostream>
using namespace std;

//function prototypes
 int change1(float);
 int change2(float,float);
 int change3(float,float);
 int change4(float,float);
 
 int main()
 {
	 //declare variables
	 float Purchase=0.0;
	 float Tendered=0.0;
	 float Takings=0.0;
	 int Quarter=0;
     int Dime=0;
	 int Nickel=0;

	 //enter input values
	 cout<<"Puchase :";
	 cin>>Purchase;
	 cout<<"Amount Tendered:";
	 cin>>Tendered;
	 
	 //calculate
    Takings= Tendered-Purchase;

	//display
	 
    cout<<"Quarters:"<<change1(Takings)<<endl;
	cout<<"Dimes:"<<change2(Takings,Quarter)<<endl;
	cout<<"Nickels:"<<change3(Quarter,Dime)<<endl;
	cout<<"Cents:"<<change4(Dime,Nickel)<<endl;
	
	return 0;
	} // end of main function

 //*****program-defined functions*****
 int change1(float Takings)
 { 
	 int Quarter=0;
     Quarter=(Takings/0.25);
     return Quarter;
 }

 int change2(float Takings,float Quarter)
 {  
	 int Dime=0;
	 Dime=(Takings-Quarter)/.1;
	 return Dime;
 }
 int change3(float Quarter,float Dime)
 {
	 int Nickel=0;
	 Nickel=(Quarter-Dime)/0.5;
	 return Nickel; 
 }
 int change4(float Dime,float Nickel)
 {
	 int Cent=0;
	 Cent=(Dime-Nickel)/0.01;
	 return Cent;
 }

Sample output
Purchase=3.08
Tendered=4
Quarters=3
Dimes=1
Nickels=1
Cents=2
Quarters=

Dave Sinkula commented: Use code tags. +0

Recommended Answers

All 2 Replies

Two basic problems I see:
1) You don't initialize quarter, dime, and nickle to anything.
2) quarter, dime, and nickle are floating point numbers, so you'll want to be careful that they only contain integer values.

In addition to that, you may want to make sure that Takings is not negative, so they aren't cheating you.

And, if I could offer some 'style' suggestions:

a) names like 'change1()' and 'change2()' don't tell the reader anything about what they DO. In my humble opinion, function/method names should describe what they DO, and variables should describe what they ARE.

b) There is a wonderful opportunity here to make a COMMON routine that takes as an argument the total amount and the denomination of the coin you want it to process. Something like:

int HowManyCoins( float dollarAmount, int coinValueInCents )
{
    return (int)(dollarAmount * 100.0) / coinValueInCents;
}

Or, maybe:

void HowManyCoins( float dollarAmount, int coinValueInCents, int& coinsReturned, float& remainingMoney )
{
    coinsReturned = (int)(dollarAmount * 100) / coinValueInCents;
    remainingMoney = dollarAmount - (double)(coinsReturned * coinValueInCents);
}
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.