Thread: problem with some simple code =/

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    26

    problem with some simple code =/

    Ok so I'm trying to write a console app that will take from the user the amount of money due and the amount of money tendered and make change. One of the requirments was to use reference parameters just incase anyone wonders why theyre all over. I have written the code and it works fine right up until you need more than one dime, nickel, penny, or combination therof. For example if one nickel or one dime is due in change it works fine. If one nickel AND one dime is due in change though the program hangs. Also hangs if 2 nickels, 2 dimes, or 2 pennies are due. I tried printing the values before the print function runs just to make sure it was in the change making function which it was. I see no error in the code of that function though. All I can think is that there is some problem with the 0's in the floating point values and I am somehow losing the prcision or something and causing an infinite loop. Anyways, thanks in advance for reading and any help is much appreciated.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int makeChange(int& fifties, int& twenties, int& tens, int& fives, int& ones, int& quarters, int& dimes, int& nickels, int& pennies, double& change);
    int printChange(int& fifties, int& twenties, int& tens, int& fives, int& ones, int& quarters, int& dimes, int& nickels, int& pennies, double& originalChange);
    
    int main(void) {
    	int quit, fifties, twenties, tens, fives, ones, quarters, dimes, nickels, pennies;
    	double due, tendered, change, originalChange;
    
    	quit = 0;
    
    	cout.setf(ios::fixed | ios::showpoint);
    	cout.precision(2);
    
    	cout << endl << "Welcome to the change maker!" << endl
    		 << "This program will accepts the amount due and the amount tendered from the user \nand computes the change due." << endl;
    
    	//start the menu
    	while(quit != 2) {
    		
    		cout << endl << "Please choose from the following options:" << endl
    			 << "1) Compute change." << endl
    			 << "2) Quit." << endl;
    
    		cin >> quit;
    
    		//if the user selects compute change
    		if(quit == 1) {
    			//init all values back to 0 each time a new transaction begins
    			fifties = twenties = tens = fives = ones = quarters = dimes = nickels = pennies = 0;
    			due = tendered = change = originalChange = 0.00;
    
    			//prompt the user for the amount due and validate to make sure its greater than 0
    			cout << "Please enter the amount due:" << endl;
    			cin >> due;
    			if(due <= 0.00) {
    				cout << "Error:  The amount due must be greater than 0.00" << endl;
    			}
    
    			//prompt the user for the amount tendered and validate to make sure its greater than 0
    			cout << "Please enter the amount tendered:" << endl;
    			cin >> tendered;
    			if(tendered <= 0.00) {
    				cout << "Error:  The amount tendered must be more than 0.00" << endl;
    			}
    
    			//find the change due by subtracting due from tendered.  Validate the change to make sure its greater than 0
    			change = tendered - due;
    			if(change == 0.00) {
    				cout << "Change due is 0.00.  Nothing to compute." << endl;
    			} else if(change < 0.00) {
    				cout << "Error:  The amount tendered must be more than the amount due." << endl;
    			} else {
    				//if change is due
    				//store a copy of change due for the final print
    				originalChange = change;
    				//run the makechange function passing the denominations and change due by reference
    				makeChange(fifties, twenties, tens, fives, ones, quarters, dimes, nickels, pennies, change);
    
    				/*some leftover code I was using to decide if a problem was in makeChange or printChange
    				cout << fifties << endl
    					 << twenties << endl
    					 << tens << endl
    					 << fives << endl
    					 << ones << endl
    					 << quarters << endl
    					 << dimes << endl
    					 << nickels << endl
    					 << pennies << endl;*/
    
    				//run the printChange function passing the denominations and the originalChange by reference
    				printChange(fifties, twenties, tens, fives, ones, quarters, dimes, nickels, pennies, originalChange);
    			}
    
    			//validate the menu choice to make sure a 1 or a 2 was selected
    		} else if((quit != 1) && (quit != 2)) {
    			cout << "Error:  You have entered an invalid menu option." << endl;
    		}
    	}
    	return 0;
    }
    
    //the makeChange function is basically just a loop that tests the ammount of change due to see what the greatest 
    //denomination is that can be subtracted from it.  When it finds that denomination it subtracts the value of that
    //denomination and incriments the denominations counter by 1.  Then loops again.  It continues looping until the
    //change due is 0.00.
    
    int makeChange(int& fifties, int& twenties, int& tens, int& fives, int& ones, int& quarters, int& dimes, int& nickels, int& pennies, double& change) {
    
    	while(change > 0.00) {
    		if(change >= 50.00) {
    			change -= 50.00;
    			fifties++;
    		} else if(change >= 20.00) {
    			change -= 20.00;
    			twenties++;
    		} else if(change >= 10.00) {
    			change -= 10.00;
    			tens++;
    		} else if(change >= 5.00) {
    			change -= 5.00;
    			fives++;
    		} else if(change >= 1.00) {
    			change -= 1.00;
    			ones++;
    		} else if(change >= .25) {
    			change -= .25;
    			quarters++;
    		} else if(change >= .10) {
    			change -= .10;
    			dimes++;
    		}   else if(change >= .05) {
    			change -= .05;
    			nickels++;
    		}   else if(change >= .01) {
    			change -= .01;
    			pennies++;
    		}
    	}
    	return 0;
    }
    
    //the printChange function takes the counters for each denomination and the original change as arguments.
    //It tests each counter to see if that counters value is greater than 0.  If it is greater than 0 it prints
    //the name of the denomination and the value of the counter.
    
    int printChange(int& fifties, int& twenties, int& tens, int& fives, int& ones, int& quarters, int& dimes, int& nickels, int& pennies, double& originalChange){
    	cout << endl << "The change due is: " << originalChange << endl
    		 << "Please make this change using the following: " << endl;
    
    	if(fifties != 0) {
    		 cout << fifties << " fifties" << endl;
    	}
    
    	if(twenties != 0) {
    		 cout << twenties << " twenties" << endl;
    	}
    
    	if(tens != 0) {
    		 cout << tens << " tens" << endl;
    	}
    
    	if(fives != 0) {
    		 cout << fives << " fives" << endl;
    	}
    
    	if(ones != 0) {
    		 cout << ones << " ones" << endl;
    	}
    
    	if(quarters != 0) {
    		 cout << quarters << " quarters" << endl;
    	}
    
    	if(dimes != 0) {
    		 cout << dimes << " dimes" << endl;
    	}
    
    	if(nickels != 0) {
    		 cout << nickels << " nickels" << endl;
    	}
    
    	if(pennies != 0) {
    		 cout << pennies << " pennies" << endl;
    	}
    	return 0;
    }
    Regards,
    ~Joshua Norton

  2. #2
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    Yea after stepping through, i noticed that the precision does go haywire once you get to 0.01000000000 decrementing 0.01 off of this makes it go to 0.0099999998, so i added a simple else clause which picks up on this and sets change to 0.00, this eliminates your infinite loop

    Code:
    int makeChange(int& fifties, int& twenties, int& tens, int& fives, int& ones, int& quarters, int& dimes, int& nickels, int& pennies, double& change) {
    
    	while(change > 0.00) {
    		if(change >= 50.00) {
    			change -= 50.00;
    			fifties++;
    		} else if(change >= 20.00) {
    			change -= 20.00;
    			twenties++;
    		} else if(change >= 10.00) {
    			change -= 10.00;
    			tens++;
    		} else if(change >= 5.00) {
    			change -= 5.00;
    			fives++;
    		} else if(change >= 1.00) {
    			change -= 1.00;
    			ones++;
    		} else if(change >= .25) {
    			change -= .25;
    			quarters++;
    		} else if(change >= .10) {
    			change -= .10;
    			dimes++;
    		}   else if(change >= .05) {
    			change -= .05;
    			nickels++;
    		}   else if(change >= .01) {
    			change -= .01;
    			pennies++;
    		} else if(change <= .01) {
    			change = 0.00;
    		}
    	}
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    26

    ...

    Thanks. This avoided the error. I added pennies++ to your addition to account for the fact that .0099 does represent the last penny.

    Thanks again!
    Regards,
    ~Joshua Norton

  4. #4
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92

    Wink

    oops missed that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using java programs within C code
    By lemania in forum Linux Programming
    Replies: 1
    Last Post: 05-08-2005, 02:02 AM
  2. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 01:30 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM