1. ## 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. 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;
}```

3. 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. Thankyou that works great

I never usually use this what does it do?

return 0;

Andy

5. 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.

6. short answer: return 0; ends the function