Thread: Beginner C++ Program Help

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    6

    Beginner C++ Program Help

    Hey everyone,

    I am new to this forum and was hoping someone could help me out quickly. I am currently taking my first computer programming class (Introduction to C++ Programming) and am stumped on this one program.

    What we are assigned to do is ask the user for a value they want back in change (this variable will then be outputted as how many toonies ($2.00), loonies ($1.00), quarters, dimes, nickles and pennies to return.

    I have figured it all out but when I get to the pennies I am always one short. If you need the code let me know and I will post it.

    I hope I can contribute to this forum someday

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Yes, we'd have to see the code, otherwise how can we know where you went wrong?

    I'm guessing there's more to it, but why don't you just programmatically add another penny to the user's input?

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    6
    Here is my code. Again, should be very simple to solve my issue. I don't think just adding a cent would work because then it would be one cent over in some cases (say the user puts in 5.00.

    Code:
    #include <iostream>
    using namespace std;
    int main ()
    {
    	float change = 0.0;
    	int toonies = 0;
    	int loonies = 0;
    	int quarters = 0;
    	int dimes = 0;
    	int nickel = 0;
    	int pennies = 0;
    
    	cout<<”How much change is to be returned? “;
    	cin>>change;
    
    	toonies=(change/2);
    	cout<<toonies<<” X $2.00”<<endl;
    	change=change-(toonies*2);
    
    	loonies=change;
    	cout<<loonies<<” X $1.00”<<endl;
    	change=change-loonies;
    
    	quarters=change/0.25;
    	cout<<quarters<<” X $0.25”<<endl;
    	cout=change-(quarters*0.25);
    
    	dimes=change/0.1;
    	cout<<dimes<<” X $0.10”<<endl;
    	change=change-(dimes*0.1);
    
    	nickel=change/0.05;
    	cout<<nickel<<” X $0.05”<<endl;
    	change=change-(nickel*0.05);
    
    	pennies=change/0.01;
    	cout<<pennies<<” X $0.01”<<endl;
    
    	return 0;
    }
    And when running the program this is what is outputted:

    How much change is to be returned? (I enter 5.79)
    2 X $2.00
    1 X $1.00
    3 X $0.25
    0 X $0.10
    0 X $0.05
    3 X $0.01
    Last edited by jensklemp; 01-28-2010 at 04:13 PM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Instead of using dollars, use pennies as your base of calculation. The problem is that floating point math in C/C++ is inherently unreliable.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    6
    Thanks a lot got it figured out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM