Thread: little help please.

  1. #16
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You're getting crazy numbers back for change because you're just working out how many quarters the change consists of, then how many dimes, etc. but you're not taking into account the fact that if you've already given back some of the change in quarters, you don't have to give the whole change back in dimes, etc.

    Also, your code didn't compile as is, I had to remove the second "} /* end while loop */.

    If you need two while loops, presumable one is so that the user can keep entering new input over and over, and the inner loop is so that the user can enter multiple items. You have inner loop done, so add the outer loop, with some condition:
    Code:
    int done = 0;
    while (1)
    {
        /* ask how many items, if they enter 0, break out of the outer while */
        while (numItems != 0)
        {
            /* the inner loop that asks the amount for each item and adds it to the total */
            numItems--;
        }
        /* the code that computes the change */
        /* the code that outputs the amount of change */
    }
    Of course, the assignment wants you to keep track of how many quarters/dimes, etc. the "bank" has, so you'll need an array, or separate counters to keep track of that, and decrease them as you use the coins appropriately. Presumably you'll need to check when they reach zero, and output that the bank has ran out of change?

  2. #17
    Registered User
    Join Date
    Sep 2005
    Posts
    16
    ahhhh, im so totally lost its not funny, could you take my code and put statements in the spots i need to write certain code for, i undertsand what you are telling me im just lost as to where it all should go.

  3. #18
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    No, I've spelled it out for you enough. Use your brain. If you don't start using it now, you'll have no hope at all if you manage to move onto even vaguely advanced problems.

  4. #19
    Registered User
    Join Date
    Sep 2005
    Posts
    16
    ok, i understand thanks for all your help. this is kind of advanced for me though, since i only started programming 2 weeks ago. but thanks again ill try and hammer it out for a while.

  5. #20
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    1st time posting...

    touched up on ur programming, it was a rushed job...

    doesnt really work (i.e. if u put 0 items on the 2nd or 3rd time round, it wont close.. read my notes at the end of the program. hope it helps u.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int numItems;
        float price, amtPaid, total = 0, change;
        int quarters = 10, dimes = 10, nickels = 10, pennies = 10, count;
    
        printf("Please enter the number of grocery items: ");
        scanf("%d",&numItems); 
    
        
        while(numItems != 0) /* main while loop, makes sure your program works only if numItems not equal to 0.*/
        {
              
            count = 0;
            /*2nd loop, causes your program to ask repeatedly for the prices of your items, until numItems is met. */
            while(count<numItems)
           {
               printf("Please enter the price for your item>");
               scanf("%f", &price);
               total += price;
               count++;
           }
           
           
           printf("\n\nPlease enter the amount paid: ");
           scanf("%f",&amtPaid);
           
           change = amtPaid - total;
           
           printf("\nChange is %.2f\n", change);
           /* I don't understand your convertions */
           quarters = change / .25;
           dimes = change / .10;
           nickels = change / .05;
           pennies = change / .01;
           /*convertions... . */
           printf("The coins returned are: \n %d Quarters \n %d Dimes \n %d Nickels \n %d Pennies.\n", quarters, dimes, nickels, pennies);
    
    
           total = 0;
            
        } 
        return 0;
    
    } /*end function main*/
    
    
    //note on my program, you have to use a clr memory function, search around the forums. you have to clr up 
    //the 'numItems' variable at the end of the loop good luck hahahah.
    //and ther may be errors, i rushed thru it. but yes, u get the rough idea.
    ahha, hope it posts right, i dont even know the tags ...

  6. #21
    Registered User
    Join Date
    Sep 2005
    Posts
    16
    hey, thx for giving it a shot, i completed the program and got it turned in on time.

  7. #22
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    Impressive, very simple, easy to read

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    //note on my program, you have to use a clr memory function, search around the forums. you have to clr up
    //the 'numItems' variable at the end of the loop good luck hahahah.
    There are too many ways to respond to this:

    You mean something like memset?
    Why don't you just set it to zero? 'numItems = 0'
    There's no such thing as "clear memory".
    You mean like simply prompting for 'numItems'?
    You mean like just prompting to continue?
    You mean like actually changing the value of 'numItems'? Because as-is, it never changes.
    You mean...

    I'm sure I've left one or two out. Someone else bored and drunk can finish the list.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #24
    Registered User
    Join Date
    Aug 2005
    Posts
    38
    I wish I could help you man... i'm new to programming too, and loops can confuse me...

  10. #25
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
            while (numItems != 0)
            {
                printf("Please enter the price for your item>");
                scanf("%f", &price);
                total += price;
                numItems--;}
            }
    You probably don't want that closing brace.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed