A nudge in the right direction please.

This is a discussion on A nudge in the right direction please. within the C++ Programming forums, part of the General Programming Boards category; Alright where to start where to start... Ah here we go. Well just to let you all know, this is ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    14

    A nudge in the right direction please.

    Alright where to start where to start...

    Ah here we go. Well just to let you all know, this is a 2 person GROUP assignment. So it is for a grade (extra credit actually).

    I am not asking you guys to DO this work for me. Just more or less try to help me with what is wrong and some simple pointers, that I just haven't really learned to implement correctly. So here goes.

    This is the infamous Change program. Where you take the cost of merchandise and then take the amount tendered, and then give change in

    I needed to make a function for CHANGE AND INPUT, which I did..

    5 dollar bills , dollars, quarters , dimes , nickels, and pennies.

    My problems so far. In order of annoyance.

    I am not asking for help by one person on all 3 , any info you could point me in would be a great help THanks ahead of time

    1)If I dont need a certain coin for change, then I shouldnt list it. So if i need just 2 quarters, it shouldn't say 0 dimes, 0 pennies ect..
    I am thinking that i need to somehow put in an if statement or somthing to say : if a value of a coin is zero, dont show it.
    Code:
    if	((change>=50)&& (change<75))
    	{
    		change=change-50;
    		quarter=quarter+2;
    	}
    My next problem is for some reason it is giving change for the first input (money) instead of the change I have left from subtracting (tendered-money)
    Code:
    while(money>=0.00)
    
    {
    	amount=money*100;
    	dollar=money;
    	change=(money-dollar)*100;
    	if (change >= 500)
    	{
    		change = dollar/500;
    		dollar5 = dollar5+1;
    	}
    	if(change>=75)
    	{
    		change=change-75;
    		quarter=quarter+3;
    	}
    I have tried switching the variables, but to really no avail...

    3)MOST ANNOYING PROBLEM.. I get the program to loop and all that good stuff but I CAN input a dollar AND CHANGE number into the first input ...(money = 1.15), but when I go to input the amount tendered , if I used a whole number it will work. HOwever when I use something like 2.20. It will
    A) not give me the correct value and B) SKIP THE NEXT : ENTER COST AND GO STRAIGHT TO THE SECOND INPUT...
    I have no idea why..I am sure this is a simple math error but for playing with thing for many many hours.

    4 ) I cannot get the 5 dollar bills to show up at all! I know its an error in here. I am multiplying or dividing something wrong...
    Code:
    amount=money*100;
    	dollar=money;
    	change=(money-dollar)*100;
    	if (change >= 500)
    	{
    		change = dollar/500;
    		dollar5 = dollar5+1;
    	}
    Last thing... In the beggining of the program I USED TO HAVE #include <iostream.h> but later changed it to <iostream> because i had to use <<fixed<< setprecision (2)...could this be a problem also.

    I d like to thank anyone that would like to help me out. I know its a lot of reading..but seriously I have been reading these posts /help/ programs you guys write and I am completely amazed by this stuff. I like C++ but since I am a newb its tough.

    Almost forgot ..
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    void CHANGE();
    void input();
    int quarter,dime,penny,dollar, nickel, amount, dollar5, tendered;
    double money, change, totalchange;
    
    int main()
    {
    	dollar=dollar5=quarter=dime=penny=nickel=0.00;
    
    input();
    CHANGE();
    while(money>=0.00)
    
    {
    	amount=money*100;
    	dollar=money;                              //problems here!!
    	change=(money-dollar)*100;
    	if (change >= 500)
    	{
    		change = dollar/500;
    		dollar5 = dollar5+1;
    	}
    	if(change>=75)
    	{
    		change=change-75;
    		quarter=quarter+3;
    	}
    
    	if	((change>=50)&& (change<75))
    	{
    		change=change-50;
    		quarter=quarter+2;
    	}
    
    	if((change>=25)&&(change<50))
    	{
    		change=change-25;
    		quarter=quarter+1;
    	}
    
    	if((change>=10)&&(change<25))
    	{
    		change=change-10;
    		dime=dime+1;
    	}
    	if(change>=10)
    	{
    		change=change - 10;
    		dime=dime+1;
    	}
    
    	if((change>=5)&&(change<10))
    	{
    		change=change-5;
    		nickel+=1;
    	}
    	if((change<5))
    	{
    	penny=penny+change+0.01;
    	}
    
    	cout<<endl;
    
    	CHANGE();
    
    	cout << fixed << setprecision(2) << " The change is:" << totalchange;
    	cout << "."<<endl;
    	cout << dollar5 <<" Five Dollars" << endl;
    	cout << dollar<<" Dollar"<<endl;
    	cout << quarter<<" Quarter"<<endl;
    	cout << dime<<" Dime"<<endl;
    	cout << nickel<<" Nickel"<<endl;
    	cout << penny<<" Penny"<<endl<<endl;
    
    	dollar5=dollar=quarter=dime=nickel=penny=tendered=0.00;           // DO i need this???
    
    	input();
    	CHANGE();
    }
    	return 0;
    }
    
    void CHANGE()
    {
    	totalchange = (tendered - money);
    }
    
    void input()
    {	
    	cout << " Enter -1 to exit the program"<<endl;
    	cout << " Enter cost of purchase :";
    	cin >> money; 
    	cout << endl;
    	cout << " Enter amount tendered :";
    	cin >> tendered;
    		
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Consider what happens when you want to give change for the amount of $7.89

    change = 789;

    First step, $5 amount
    num = change / 500;
    change = change % 500;
    if ( num > 0 ) // you need to give this number of that unit of currency


    Second step, $1 amount
    num = change / 100;
    change = change % 100;
    if ( num > 0 ) // you need to give this number of that unit of currency


    Etc Etc, until you're down to your last cent.

    Work this example through on paper to see how it works
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Hope I don't lose this like yesterday

    You are making this too complicated.

    Quote Originally Posted by ftblstr2319
    1)If I dont need a certain coin for change, then I shouldnt list it. So if i need just 2 quarters, it shouldn't say 0 dimes, 0 pennies ect..
    I am thinking that i need to somehow put in an if statement or somthing to say : if a value of a coin is zero, dont show it.
    Correct assumtion. An if statement will work fine


    Quote Originally Posted by ftblstr2319
    Code:
    if	((change>=50)&& (change<75))
    	{
    		change=change-50;
    		quarter=quarter+2;
    	}
    My next problem is for some reason it is giving change for the first input (money) instead of the change I have left from subtracting (tendered-money)
    Code:
    while(money>=0.00)
    
    {
    	amount=money*100;
    	dollar=money;
    	change=(money-dollar)*100;
    	if (change >= 500)
    	{
    		change = dollar/500;
    		dollar5 = dollar5+1;
    	}
    	if(change>=75)
    	{
    		change=change-75;
    		quarter=quarter+3;
    	}
    I have tried switching the variables, but to really no avail...
    This is really way to complex. You set up a loop while money is > 0.

    For example, for a quarter:
    If the money left is greater than 25
    -- increment the number of quarters
    -- subtract 25 from the money
    -- back to top of loop

    Do this for all different values (500,100,25,10,5)


    Quote Originally Posted by ftblstr2319
    3)MOST ANNOYING PROBLEM.. I get the program to loop and all that good stuff but I CAN input a dollar AND CHANGE number into the first input ...(money = 1.15), but when I go to input the amount tendered , if I used a whole number it will work. HOwever when I use something like 2.20. It will
    A) not give me the correct value and B) SKIP THE NEXT : ENTER COST AND GO STRAIGHT TO THE SECOND INPUT...
    I have no idea why..I am sure this is a simple math error but for playing with thing for many many hours.
    Sounds like your input is leaving the input stream dirty. What happens if you aren't careful is you read a value but the \n is left in the input stream. You may need to modify your inputting to alleviate this situation.


    Quote Originally Posted by ftblstr2319
    4 ) I cannot get the 5 dollar bills to show up at all! I know its an error in here. I am multiplying or dividing something wrong...
    Code:
    amount=money*100;
    	dollar=money;
    	change=(money-dollar)*100;
    	if (change >= 500)
    	{
    		change = dollar/500;
    		dollar5 = dollar5+1;
    	}
    Code:
    change = dollar - 500;
    maybe??


    Check the comments:
    Quote Originally Posted by ftblstr2319
    Almost forgot ..
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    void CHANGE();
    void input();
    int quarter,dime,penny,dollar, nickel, amount, dollar5, tendered;
    double money, change, totalchange;
    
    int main()
    {
    	dollar=dollar5=quarter=dime=penny=nickel=0.00;
    
    input();         // indent everything from here to the return
    CHANGE();        // for consistancy.
    while(money>=0.00)    // Don't loop if == 0
    
    {
    	amount=money*100;
    	dollar=money;                              //problems here!!
    	change=(money-dollar)*100;    // yes, move most of these out of the loop
    	if (change >= 500)
    	{
    		change = dollar/500;
    		dollar5 = dollar5+1;
    	}
    	if(change>=75)
    	{
    		change=change-75;
    		quarter=quarter+3;
    	}
    
    	if	((change>=50)&& (change<75))
    	{
    		change=change-50;
    		quarter=quarter+2;
    	}
    
    	if((change>=25)&&(change<50))
    	{
    		change=change-25;
    		quarter=quarter+1;
    	}
    
    	if((change>=10)&&(change<25))
    	{
    		change=change-10;
    		dime=dime+1;
    	}
    	if(change>=10)
    	{
    		change=change - 10;
    		dime=dime+1;
    	}
    
    	if((change>=5)&&(change<10))
    	{
    		change=change-5;
    		nickel+=1;
    	}
    	if((change<5))
    	{
    	penny=penny+change+0.01;
    	}
    
        // Rethink from here down. Should this be insode the loop?
    	cout<<endl;
    
    	CHANGE();
    
    	cout << fixed << setprecision(2) << " The change is:" << totalchange;
    	cout << "."<<endl;
    	cout << dollar5 <<" Five Dollars" << endl;
    	cout << dollar<<" Dollar"<<endl;
    	cout << quarter<<" Quarter"<<endl;
    	cout << dime<<" Dime"<<endl;
    	cout << nickel<<" Nickel"<<endl;
    	cout << penny<<" Penny"<<endl<<endl;
    
    	dollar5=dollar=quarter=dime=nickel=penny=tendered=0.00;
    
    	input();
    	CHANGE();
    }
    	return 0;
    }
    
    void CHANGE()
    {
    	totalchange = (tendered - money);
    }
    
    void input()
    {	
    	cout << " Enter -1 to exit the program"<<endl;
    	cout << " Enter cost of purchase :";
    	cin >> money; 
    	cout << endl;
    	cout << " Enter amount tendered :";
    	cin >> tendered;
    		
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    input();
    CHANGE();

    //you now have calculated totalChange

    //only proceed if totalChange > 0
    if totalChange > 0

    //change totalChange to pennies from dollars and cents

    totalChange *= 100

    //and then determine the number of
    //each denomination to give by starting with
    //dollar5, working to pennies by first asking
    //if that denomination will be used and
    //then how many to give. Maybe like this,
    //repeated for each denomination in order:

    if totalChange >= x

    //determine number of denomination x to return

    while totalChange >= x

    //increment number of denomination x by 1

    //subtract x from totalChange
    totalChange -= x

    end while totalChange >= x

    display number of denomination x to return

    end if totalChange > x

    .
    .
    .

    end if totalChange > 0

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    Hey guys thanks for all the help!!! Just like one of you said, I was making it much much too complicated. But after taking a little bit from each of your responses I was able to compile a working program that does what it was supposed to do.

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some Direction Needed
    By Bidamin in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2009, 10:15 AM
  2. Mouse Maze Problem
    By Furbiesandbeans in forum C++ Programming
    Replies: 13
    Last Post: 04-28-2008, 04:20 PM
  3. A nudge in the right direction?
    By Kendrose in forum C++ Programming
    Replies: 2
    Last Post: 12-06-2002, 09:50 PM
  4. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 12:54 AM
  5. Classic problem doesn't accept direction
    By cheesehead in forum C++ Programming
    Replies: 5
    Last Post: 11-13-2001, 05:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21