Thread: stuck on purse program.

  1. #16
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    I would like to make it a loop eventually, but first I was just trying to get the "float" part figured out.

  2. #17
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    It does not loop. It does not add/subtract correctly. I am stuck.
    Code:
      float total=0,t;
      int choice=0;	
    	printf("Would You Like to Insert or Remove Coins?\n");
    	printf("Press 1 for Insert.\n");
    	printf("Press 2 for Remove.\n");
    	scanf("%d", &choice);
    
    
    while(!choice)
    {
    } 
    if(choice == 1)
    {
    	printf( "%d pennies + %d nickels + %d dimes + %d quarters = $%.2f total\n", 
    	    	pennies, nickels, dimes, quarters);
    	scanf("%d%d%d%d", &p, &n, &d, &q);
    		pennies += p;
    		nickels += n;
    		dimes += d;
    		quarters += q;
    		total=total+(25*quarters + 10*dimes + 5*nickels + pennies/100);
    	printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%.2f total\n", 
    	    	pennies, nickels, dimes, quarters, total);
    }
    else
    {
    	printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%.2f total\n", 
    		      pennies, nickels, dimes, quarters, total);
    	scanf("%d%d%d%d", &p, &n, &d, &q);
    		pennies -= p;
    		nickels -= n;
    		dimes -= d;
    		quarters -= q;
    		total=total-(25*quarters + 10*dimes + 5*nickels + pennies/100);
    	printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%.2f\n", 
    	        pennies, nickels, dimes, quarters, total);
    }
    }

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Let's just look at one section of your code:
    Code:
    }
    if(choice == 1)
    {
        printf( "%d pennies + %d nickels + %d dimes + %d quarters = $%.2f total\n",
                pennies, nickels, dimes, quarters);
        scanf("%d%d%d%d", &p, &n, &d, &q);
            pennies += p;
            nickels += n;
            dimes += d;
            quarters += q;
            total=total+(25*quarters + 10*dimes + 5*nickels + pennies/100);
        printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%.2f total\n",
                pennies, nickels, dimes, quarters, total);
    }
    In the above code your first printf():
    Code:
        printf( "%d pennies + %d nickels + %d dimes + %d quarters = $%.2f total\n",
                pennies, nickels, dimes, quarters);
    You have 5 format specifiers, yet you only have 4 parameters. Always pass the proper number of parameters to printf(). At this point there is no use in printing total because you haven't computed the total yet.

    Next why do you have all the "temporary" variables, p,n,q and d? Just use your pennies, nickles, quarters and dimes in your scanf().

    Next when you do math with integers there are no fractions. Therefore in your total calculation you will have problems. The variable pennies is an int, the constant 100 is an int therefore this will yield an int value, no fractions. So if pennies is less than 100 you will get zero. Your total calculation should look like:
    Code:
     total=total+(25*quarters) + (10*dimes) + (5*nickels) + pennies);
    Once you have all the values added together and placed in your floating point number you can then do the division:
    Code:
    total = total / 100.0
    Jim

  4. #19
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    We posted at same time!
    When I enter $1.41 in change inserted, I get back the total $141.00 (should be $1.41).
    When I enter $1.41 in change removed, I get back the total $141.00 (should be $0.00).

    Why is this? Thanks.

    Code:
    #include <stdio.h>
    #include <math.h>
    int main(void)
      {   
      int pennies=0, nickels=0, dimes=0, quarters=0, p,n,d,q;
      float total=0,t;
      int choice=0;    
        printf("Would You Like to Insert or Remove Coins?\n");
        printf("Press 1 for Insert.\n");
        printf("Press 2 for Remove.\n");
        scanf("%d", &choice);
    
    
    if(choice == 1)
    {
        printf( "%d pennies + %d nickels + %d dimes + %d quarters = $%.2f total\n", 
                pennies, nickels, dimes, quarters, total);
        scanf("%d%d%d%d", &p, &n, &d, &q);
            pennies += p;
            nickels += n;
            dimes += d;
            quarters += q;
            total=total+(25*quarters + 10*dimes + 5*nickels + pennies);
        printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%.2f total\n", 
                pennies, nickels, dimes, quarters, total);
    }
    else
    {
        printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%.2f total\n", 
                  pennies, nickels, dimes, quarters, total);
        scanf("%d%d%d%d", &p, &n, &d, &q);
            pennies -= p;
            nickels -= n;
            dimes -= d;
            quarters -= q;
            total=total-(25*quarters + 10*dimes + 5*nickels + pennies);
        printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%.2f total\n", 
                pennies, nickels, dimes, quarters, total);
    
    
    }
    }
    Last edited by rosemary; 10-01-2012 at 11:37 AM.

  5. #20
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Quote Originally Posted by jimblumberg View Post
    At this point there is no use in printing total because you haven't computed the total yet.
    I put that because I thought I would want to show the beginning balance of my purse (if I ever get this into a loop).

  6. #21
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    When I enter $1.41 in change inserted, I get back the total $141.00 (should be $1.41).
    You never do the division. Look closely at my last post.

    When I enter $1.41 in change removed, I get back the total $141.00 (should be $0.00).
    Actually you should get a value of $-1.41 not zero. Remember total is starting at zero. But look at this calculation:
    Code:
    total=total-(25*quarters + 10*dimes + 5*nickels + pennies);
    Also remember that quarters, dimes, nickels, and pennies will be negative. What happens if you subtract a negative value from zero?

    I put that because I thought I would want to show the beginning balance of my purse (if I ever get this into a loop).
    Then you need to add total to the parameters. Remember printf() can really cause problems if you don't pass the correct number of parameters for the number of specifiers.

    Jim

  7. #22
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Yes, we posted at the same time so I hadn't seen your post on total/100 after the point. So, using that, I get the correct total when inserting money.
    For removing money, I can get the correct answer if I use total/-100, but is that an incorrect way to do it?

  8. #23
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Code:
    #include <stdio.h>
    #include <math.h>
    int main(void)
      {   
      int pennies=0, nickels=0, dimes=0, quarters=0, p,n,d,q;
      float total=0,t;
      int choice=0;	
    	printf("Would You Like to Insert or Remove Coins?\n");
    	printf("Press 1 for Insert.\n");
    	printf("Press 2 for Remove.\n");
    	scanf("%d", &choice);
    
    
    if(choice == 1)
    {
    	printf( "%d pennies + %d nickels + %d dimes + %d quarters = $%.2f total\n", 
    	    	pennies, nickels, dimes, quarters, total);
    	scanf("%d%d%d%d", &p, &n, &d, &q);
    		pennies += p;
    		nickels += n;
    		dimes += d;
    		quarters += q;
    		total=total+(25*quarters) + (10*dimes) + (5*nickels) + pennies;
    		total=total/100;
    	printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%.2f total\n", 
    	    	pennies, nickels, dimes, quarters, total);
    }
    else
    {
    	printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%.2f total\n", 
    		      pennies, nickels, dimes, quarters, total);
    	scanf("%d%d%d%d", &p, &n, &d, &q);
    		pennies -= p;
    		nickels -= n;
    		dimes -= d;
    		quarters -= q;
    		total=total-(25*quarters) - (10*dimes) - (5*nickels) - pennies;
    		total=total/-100;
    	printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%.2f total\n", 
    	        pennies, nickels, dimes, quarters, total);
    
    
    }
    }

  9. #24
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Yes, it really is incorrect. In my opinion you should not use negative numbers with your individual coins, how can you have negative coins? By dealing with only positive coins your calculation would be correct:
    Code:
    total = total - ((25 * quarters) + (10 * dimes) + (5 * nickels) + pennies));
    I also suggest that you use parentheses when using division/multiplication and addition/subtraction. They will make your code much easier to follow. Also a little white space also helps readability.

    Jim

  10. #25
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    I figured out I needed to change it to +- above and then divide by 100 for the removal. It gives me negative numbers now.
    I have negative coins and an awkward layout because I am only in week 4 of of programming
    I

  11. #26
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I have negative coins and an awkward layout because I am only in week 4 of of programming
    The amount of time programming really has noting to do with negative coins, it's really just common sense.

    Jim

  12. #27
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    i understand that. i just dont know how to do it.

  13. #28
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    if i'm not doing a loop, then there would be no point to the "removed" function if i didn't allow it to go negative. it would always be zero. i'm going to try to figure out a loop. thanks for all the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stuck in a MPI program.
    By jeffwang in forum C Programming
    Replies: 2
    Last Post: 01-05-2012, 06:33 PM
  2. Program Error has me stuck
    By Tester84 in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2010, 02:04 PM
  3. Stuck on a program
    By Northstar in forum C Programming
    Replies: 4
    Last Post: 10-12-2007, 03:02 AM
  4. Interesting program, but stuck somewhere
    By Mahesh in forum C Programming
    Replies: 5
    Last Post: 04-27-2005, 09:43 AM
  5. Stuck On Program
    By Robert Parker in forum C++ Programming
    Replies: 5
    Last Post: 06-04-2002, 01:41 PM