Like Tree1Likes

stuck on purse program.

This is a discussion on stuck on purse program. within the C Programming forums, part of the General Programming Boards category; hey guys- yes, this is a homework assignment. i'm stuck and if anyone can point me in the right direction ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    99

    stuck on purse program.

    hey guys-
    yes, this is a homework assignment. i'm stuck and if anyone can point me in the right direction it would be much appreciated. i need a program to remove and insert coins into a purse. thanks.

    Code:
    #include <stdio.h>
    #include <math.h>
    int main(void)
      {   
      int pennies=0, nickels=0, dimes=0, quarters=0, total=0, p,n,d,q,x;
      int choice;
    
    
    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 = $%d \n", pennies, nickels, dimes, quarters, total);
    scanf("%d%d%d%d", &p, &n, &d, &q);
    pennies += p;
    nickels += n;
    dimes += d;
    quarters += q;
    float x = pennies + 5*nickels + 10*dimes + 25*quarters;
    x =  (float)x/100;
    printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", pennies, nickels, dimes, quarters, total);
    }
    else (choice == 2)
    {
    printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", pennies, nickels, dimes, quarters, total);
    scanf("%d%d%d%d", &p, &n, &d, &q);
    pennies -= p;
    nickels -= n;
    dimes -= d;
    quarters -= q;
    float x = pennies + 5*nickels + 10*dimes + 25*quarters;
    x =  (float)x/100;
    printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", pennies, nickels, dimes, quarters, total);
    }
    
    
    return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,708
    My first suggestion would be to find an indentation style you like and use it consistently. This will make following your program logic much easier.

    The next suggestion would be to ask specific questions based on the information you provide. Do you have some kind of question or problem with the code you provided?



    Jim

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    First thing, I am not getting a correct total. It will show the correct number of coins, but the total reads $0.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    187
    so what is the real problem with your code at least give some basic errors so i can help...

  5. #5
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    I think your code does not compile.


    Anyway, it is wrong
    Code:
    if (choice == 1)
    {
        /* stuff */
    }
    else (choice == 2)
    {
        /* stuff */
    }
    You should write that as
    Code:
    if (choice == 1)
    {
        /* stuff */
    }
    else /* choice == 2 (unless the user didn't follow instructions) */
    {
        /* stuff */
    }

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,708
    That is probably being caused because you have defined x as an int, and an int has no fractions. So this line is probably the problem.
    Code:
    x =  (float)x/100;
    What did you input into the program? Are you inserting into the purse or extracting? How many of each coin did you enter?



    Jim

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Quote Originally Posted by jimblumberg View Post
    So this line is probably the problem.
    Code:
    x =  (float)x/100;
    Yes, but I do not know how to adjust this.

    What did you input into the program? Are you inserting into the purse or extracting? How many of each coin did you enter?Jim
    I tried inserting. I entered
    Code:
    1+2+3+4
    It displayed
    Code:
    1 pennies+2 nickels+3 dimes+4 quarters=$0

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    K. I moved float to the top, indented, and changed to just else.
    Code:
    #include <stdio.h>
    #include <math.h>
    int main(void)
      {   
      int pennies=0, nickels=0, dimes=0, quarters=0, p,n,d,q;
      int choice=0;
    
    
    float(x) = 25*quarters + 10*dimes + 5*nickels + pennies;
    x =  x/100;
    
    
    	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 = $%d \n", pennies, nickels, dimes, quarters, x);
    	scanf("%d%d%d%d", &p, &n, &d, &q);
    		pennies += p;
    		nickels += n;
    		dimes += d;
    		quarters += q;
    	printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", pennies, nickels, dimes, quarters, x);
    }
    else
    {
    	printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", pennies, nickels, dimes, quarters, x);
    	scanf("%d%d%d%d", &p, &n, &d, &q);
    		pennies -= p;
    		nickels -= n;
    		dimes -= d;
    		quarters -= q;
    	printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", pennies, nickels, dimes, quarters, x);
    }
    
    
    return 0;
    
    
    }

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by rosemary View Post
    First thing, I am not getting a correct total. It will show the correct number of coins, but the total reads $0.
    That's because you never change the value of "total" (it is initialized with 0).

    Bye, Andreas

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    ugh. the indentation didnt copy properly

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    2,708
    K. I moved float to the top, indented, and changed to just else.
    What good does moving the calculation to the beginning, before you enter any values, do you?

    Does the following even compile?
    Code:
    float(x) = 25*quarters + 10*dimes + 5*nickels + pennies;
    x =  x/100;
    Where is x defined? What type of variable is x?

    Jim

  12. #12
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    [QUOTE=jimblumberg;1125437

    Where is x defined? What type of variable is x?

    Jim[/QUOTE]

    Code:
    #include <stdio.h>
    #include <math.h>
    int main(void)
      {   
      int pennies=0, nickels=0, dimes=0, quarters=0, total=0, p,n,d,q,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 = $%.2d 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 = $%.2d total\n", pennies, nickels, dimes, quarters, total);
    }
    else
    {
    	printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%.2d 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 = $%.2d total\n", pennies, nickels, dimes, quarters, total);
    }
    
    
    return 0;
    
    
    }

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    2,708
    So does your modified code solve your problems? If not maybe you need to ask another question.

    Jim

  14. #14
    Registered User
    Join Date
    Oct 2012
    Location
    Gurgaon, Haryana, India
    Posts
    1
    one more point, keep it in a loop. Your program would exit just after single iteration.

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    2,282
    Quote Originally Posted by varshneyabhi View Post
    one more point, keep it in a loop. Your program would exit just after single iteration.
    This is a homework exercise, so a single iteration is sufficient - unless the assignment calls for the program to loop, which does not seem to be the case.

Page 1 of 2 12 LastLast
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, 05:33 PM
  2. Program Error has me stuck
    By Tester84 in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2010, 01: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

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