C++ Outputting the sum and product of a set of numbers

Beginner C++ student here, first programming class. I am trying to build a program which will accept a set of numbers from the user and output the sum and product of those numbers. Example:

Enter # of values to compute: 4

Enter number: 2

Enter number: 3

Enter number: 4

Enter number: 0.5

Sum is: 9.5, product is: 12

With what I have so far I get the correct sum, however, the product is zero no matter what I try. I am lost in getting that to work. Any help greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#include<iostream>

using namespace std;

int main(){


	double sum = 0;
	double product = 0;
	double number;
	unsigned numberitems;


	cout << "Enter number of items: ";
	cin >> numberitems;

	for (unsigned i = 0; i < numberitems; i++)
	{
		cout << "Enter number: ";
		cin >> number;

		sum += number;

		product *= number;

	}

	cout << "the sum is: " << sum << ", the product is: " << product << endl;


}
Hi,

You initialised product to zero, so it will always be zero on line 25.

It's great you initialised most of your variables, but this demonstrates why zero is not always a good choice. Assign the value of the first number to product instead.

Good Luck !!
Thank you for that! That did it. In one version of this I had initialized it by 1, but I think I had something incorrect in the code elsewhere which was giving me an incorrect value, which threw me off and I thought initializing at 1 was the incorrect choice here.

However, it all makes sense now. Plus I gotta be more careful. Again, thank you!
Topic archived. No new replies allowed.