Thread: Newbie calculating problem

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    8

    Newbie calculating problem

    Hey,

    I am very new to this and am trying to allow a user to enter 10 entries and have it calculate what the total is....

    The code I have so far is...

    Code:
    /* Product calculating */
    
    #include <stdio.h>				
    #include <stdlib.h>										
    void main()
    
    {
    int loop = 1, product;
    
    do
        { 
        printf("Please enter the price for product number %d: ", loop);
        ++loop;
    	scanf("%d",&product);
    	printf("The amount of the product you just entered was %d\n",product);
        }while (loop <=10);
    	  
    
    }
    At the moment is does...

    Please enter the price for product number 1: 123
    The amount of the product you just entered was 123
    and so on...

    I would like it to say
    Please enter the price for product number 1: 123
    The amount of the product you just entered was 123
    The total you have entered so far is 123

    I would like to know what I need to put in the loop to have a total line. I have tried lots of combinations of &total = &total + product and things like that but am having no luck.

    Thankyou for any help.

    Andy

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You need another variable to store the total (initialized to 0) and then you need to increment this variable by the current value in product each time through the loop and display it.

    Code:
    #include <stdio.h>				
    #include <stdlib.h>										
    int main()
    {
        int loop = 1, product;
        int total = 0;
    
        do
        { 
            printf("Please enter the price for product number %d: ", loop);
            ++loop;
            scanf("%d",&product);
            printf("The amount of the product you just entered was %d\n",product);
            total += product;
            printf("The total you have entered so far is %d\n",total);
        }while (loop <=10);
    	  
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So create a
    int total = 0;

    Then inside the loop do
    total = total + product;

    Then when the loop ends (or even inside it), print total in the same way as you do product.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    8
    Thankyou that works great

    I never usually use this what does it do?

    return 0;

    Andy

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Andy123
    I never usually use this what does it do?

    return 0;
    Functions return a value. The main function is supposed to return an int and not be of type void. The return statement returns a value from the function. Since main returns an int, we return an int (in this case 0). 0 is often used as an "Everything went OK" kind of return value for functions although for this simple example you could return any integer value that you want since it is likely ignored anyways.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    10
    short answer: return 0; ends the function

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  2. calculating postfix problem
    By Axolotl in forum C Programming
    Replies: 2
    Last Post: 04-01-2004, 08:49 PM
  3. Newbie opengl display problem.
    By godhand in forum Game Programming
    Replies: 1
    Last Post: 03-29-2004, 07:25 AM
  4. Problem with code (newbie question)
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-31-2002, 01:39 AM
  5. newbie coding problem
    By rippascal in forum C++ Programming
    Replies: 10
    Last Post: 01-08-2002, 11:45 PM