little help please.

This is a discussion on little help please. within the C Programming forums, part of the General Programming Boards category; I am having trouble with a loop. i can get the program to ask for the quantity and compute the ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    16

    Post little help please.

    I am having trouble with a loop. i can get the program to ask for the quantity and compute the same price for a certain amount of items, but i need it to loop and ask for the price of number(n) of items input by the user. it needs to be set up where they enter a number of items (n)and the program lets them enter the prices on that certain amount of items (n). can someone pease give me a little help with the loop. i used notes and slides i was given in class to do what i have so far but he hasnt really gone over loops that much and im new to programming.




    Code:
    /*   Cash register simulator with change machine */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    int main( void )
    {
       int items;        /* item qty purchased */
       double price;        /* item price */
       double itemTotal;    /* item total price */
       double total = 0;    /* customer total bill */
       double amtPaid;      /* amount paid by customer */
       double change;       /* change due customer */
       int centsLeft;       /* cents left to pay in change */
       
       printf("     checkout line\n\n"
          
          "Enter 0 for items to see total bill.\n\n");
          
       printf("enter number of items:    ");                           /* input */
       scanf("%d", &items);
       
       while( items != 0 )
       {
          printf("Price:       ");
          scanf("%lf", &price);
          
          itemTotal = items * price;            
          printf("Item total:  %.2f", itemTotal);         /* display totals */         
          total += itemTotal;                             /* add to total bill */
          
          printf("\n\nenter number of items:    ");                    /* working input */
          scanf("%d", &items);
       }   
       printf("\n--------------------------\n");          /* totals */
       printf("Customer total:   %8.2f\n", total);
       printf("Total due:        %8.2f\n\n", total );
    
     
       printf("\n\n");   
       system("PAUSE");
       return 0;
    }
    Last edited by Uber Fr0g; 10-10-2005 at 11:11 AM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Is this homework?

    These two lines are printing the same variable:
    Code:
       printf("Customer total:   %8.2f\n", total);
       printf("Total due:        %8.2f\n\n", total );
    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.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    16
    yeah, homework, i took that other line out lol i just noticed it.

    Im not asking for someone to do it for me, just get me on the right track as how to use a loop to do what i need done.
    Last edited by Uber Fr0g; 10-09-2005 at 07:55 PM.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    16
    can someone just give me a hint....please.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So what exactly are you stuck on? You do realise that you're asking us to:
    a) Read your entire homework problem.
    b) Read you entire program.
    c) Figure out what you have and haven't put in your program.
    d) Tell you.

    Why again would I do all that work?


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

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Quote Originally Posted by quzah
    So what exactly are you stuck on? You do realise that you're asking us to:
    a) Read your entire homework problem.
    b) Read you entire program.
    c) Figure out what you have and haven't put in your program.
    d) Tell you.

    Why again would I do all that work?


    Quzah.
    Because your time is obviously less valuable than the original poster's, silly!

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    16
    Quote Originally Posted by quzah
    So what exactly are you stuck on? You do realise that you're asking us to:
    a) Read your entire homework problem.
    b) Read you entire program.
    c) Figure out what you have and haven't put in your program.
    d) Tell you.

    Why again would I do all that work?


    Quzah.

    ^^ man, take it easy im new to all this.


    I am having trouble with the loops. i can get it to ask for the quantity and compute the same price for a certain amount of items, but i need it to loop and ask for the price of number of items input by the user. it need to be set up where they enter a number of items and the program lets them enter the prices on that certain amount of items. can someone pease give me a little help with the loop. i used notes and slides i was given in class to do what i have so far but he hasnt really gone over loops that much and im new to programming.



    My bad, i put what i needed help on though.
    Last edited by Uber Fr0g; 10-10-2005 at 11:12 AM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    prompt for number of items
    read number of items
    while some counter is less than the number you just read in
        ask for a price
        read a price
        ask for the amount
        read the amount
        do whatever it is you're doing with whatever you've just read
    Something like that?


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

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    16
    exactly, i need the inner loop to be a counter based on the number of items they enter, and read out the corresponding information.

    like this sample i made up:
    Code:
    Sample Program Execution 
    Please enter the number of grocery items> 4 
    Please enter the price of item #1> 3.50 
    Please enter the price of item #2> 2.50 
    Please enter the price of item #3> 0.75 
    Please enter the price of item #4> 1.25 
    Please enter the amount paid for the groceries> 10.00 
    The coins returned are: 
    8 quarters 
    0 dimes 
    0 nickels 
    0 pennies 
    Please enter the number of grocery items> 2 
    Please enter the price of item #1> 0.40 
    Please enter the price of item #2> 0.43 
    Please enter the amount paid for the groceries> 1.00 
    The coins returned are: 
    0 quarters 
    1 dimes 
    1 nickels 
    2 pennies 
    Please enter the number of grocery items> 1 
    Please enter the price of item #1> 4.00 
    Please enter the amount paid for the groceries> 5.00 
    The coins returned are: 
    2 quarters 
    5 dimes 
    0 nickels 
    0 pennies 
    Please enter the number of grocery items> 3 
    Please enter the price of item #1> 10.00 
    Please enter the price of item #2> 9.00 
    Please enter the price of item #3> 0.20 
    Please enter the amount paid for the groceries> 20.00 
    The coins returned are: 
    0 quarters 
    4 dimes 
    8 nickels 
    0 pennies 
    Please enter the number of grocery items> 0 
    Goodbye.
    but im lost as to how to utilize the counter inside the loop to do that.

    he also wants us to keep a runnign bank with 10 of each coin, so that when 1 runs out the others are used to make change. but i can probably figure that out if i get past the counter/loop inside my big outer loop.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I just showed you how to do it. I practically wrote the code for you. Just turn those lines of text into working code. It's not hard.


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

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    16
    man, like i said i just started programming 2 weeks ago, most of what i already have came from an example out of the book. i understand somewhat what you explained to me, but i dont know where to insert it in the code, or even how to go about doing it. I hate to sound like a noob asking you to do it for me, i know i cant learn from someone doing it for me, but i just cant figure it out.

  12. #12
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    As you said, you can't learn from someone doing it for you, so...

    Quote Originally Posted by Quzah
    prompt for number of items
    read number of items
    while some counter is less than the number you just read in
    ask for a price
    read a price
    ask for the amount
    read the amount
    do whatever it is you're doing with whatever you've just read
    Code:
    int num_items = 0;
    int count = 0;
    printf("Enter number of items: ");
    scanf("%d", &num_items); /* this doesn't check for bad input, fix in your code */
    while (/* appropriate condition */)
    {
        /* ask for a price */
        /* read a price */
        /* ask for the amount */
        /* read the amount */
        /* do whatever it is that you're doing with whatever you've just read */
        /* increment count */
    }
    That's about as far as I can go without just doing the whole thing. Post an attempt.

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    16
    i changed up the code a bit to clarify it better, but still the same basic thing. i tried a while loop inside it to get this output wich is what i need. Im not sure what kind of condition i should use for the while counter....so i didnt do it. well, i did but it never even tried to attempt the counter.
    Code:
    Sample Program Execution 
    Please enter the number of grocery items> 4 
    Please enter the price of item #1> 3.50 
    Please enter the price of item #2> 2.50 
    Please enter the price of item #3> 0.75 
    Please enter the price of item #4> 1.25 
    Please enter the amount paid for the groceries> 10.00
    but im still not getting what i need. heres the new code.
    Code:
    #include <stdio.h>
    
     
    
    int main()
    
    { /* Begin function main*/
    
     
    
          int numItems;
    
          float price, amtPaid, total = 0, change;
    
          int quarters = 10, dimes = 10, nickels = 10, pennies = 10;
    
     
    
          printf("Please enter the number of grocery items>");
    
          scanf("%d", &numItems);
    
          while (numItems != 0){
    
     
    
          /* Begin while loop*/
    
          while (numItems != 0){
    
                printf("Please enter the price for your item>");
    
                scanf("%f", &price);
    
                total += price;
    
                numItems--;}
    
     
    
          printf("\nPlease enter the amount paid>");
    
          scanf("%f", &amtPaid);
    Last edited by Uber Fr0g; 10-10-2005 at 10:20 PM.

  14. #14
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    *sigh*

    First of all, here is your code with more readable indentation:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int numItems;
        float price, amtPaid, total = 0, change;
        int quarters = 10, dimes = 10, nickels = 10, pennies = 10;
    
        printf("Please enter the number of grocery items>");
        scanf("%d", &numItems);
    
        while (numItems != 0)
        {
            /* Begin while loop*/
            while (numItems != 0)
            {
                printf("Please enter the price for your item>");
                scanf("%f", &price);
                total += price;
                numItems--;}
            }
    
            printf("\nPlease enter the amount paid>");
            scanf("%f", &amtPaid);
    Why do you have two while loops? I gave you the skeleton of the code before. while (numItems != 0) is a fine start, numItems--; is also a good idea. The way you've done it, you don't need a separate counter. So again, why have you got two while loops? You only need one? Fix the code up, post the complete program, and try to post with decent indentation also.

    Edit:

    And, adapted to your loop method, my fragment becomes:
    Code:
    while (numItems != 0)
    {
        /* ask for a price */
        /* read a price */
        /* ask for the amount */
        /* read the amount */
        /* do whatever it is that you're doing with whatever you've just read */
        numItems--;
    }
    
    /* now that the loop has finished, display the totals here */
    That's also fine.
    Last edited by cwr; 10-10-2005 at 10:35 PM.

  15. #15
    Registered User
    Join Date
    Sep 2005
    Posts
    16
    My professor asked for us to use 2 loops. it was in the problem i had on here yesterday but i did away with it due to complaints.

    here is the complete code, im getting crazy numbers back for change due.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int numItems;
        float price, amtPaid, total = 0, change;
        int quarters = 10, dimes = 10, nickels = 10, pennies = 10;
    
        printf("Please enter the number of grocery items>");
        scanf("%d", &numItems);
    
        while (numItems != 0)
        {
            /* Begin while loop*/
            while (numItems != 0)
            {
                printf("Please enter the price for your item>");
                scanf("%f", &price);
                total += price;
                numItems--;}
            }
    
            printf("\nPlease enter the amount paid>");
            scanf("%f", &amtPaid);
    
            change = amtPaid - total;
    
                   printf("change is %.2f\n", change);
    
                   quarters = change / .25;
                   dimes = change / .10;
                   nickels = change / .05;
                   pennies = change / .01;
    
             printf("The coins returned are:\n %d Quarters \n %d 
             Dimes \n %d Nickels \n%d Pennies.\n", quarters, dimes, nickels, pennies);
    
                 total = 0;
    
                     printf("\n\nPlease enter the number of grocery items>");
                     scanf("%d", &numItems);
    
      } /*end while loop*/
    
      return 0;
    
    } /*end function main*/

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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