i need help with this code using the while loop.

Write a program that will ask the user to enter a finite amount of numbers between 1 and 10 and display the product of the entered numbers.

The amount of numbers to the input will also be input by the user.
For example;

Enter the amount of numbers to be multiplied : 4

Enter a number : 6
Enter a number : 2
Enter a number : 5
Enter a number : 3

The product is 180
.
.
This is as far as i went.

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
#include <iostream>
using namespace std;
int main()
{
	int num1,num2,product,counter=1;
	
	cout<<"Enter amount of numbers to be multiplied in between 1 and 10 : "<<endl;
	cin>>num1;
	
	cout<<"Enter the numbers to be multiplied : "<<endl;
	cin>>num2;
	
	while(counter<=num1)
	{
		cin>>num2;
		product=num2*num2;
		
		
		counter++;
	}
	
	cout<<product;
	
	return 0;
}
The problem is that you multiply num2 with itself. This way, if you for example put in 7 and 8 you'll get product =7*7(=49) and in the second run product =8*8(=64). The programm will then print 64 instead of 56 to the screen. To fix this you need to multiply product with num2 (but make sure to initialise product with 1, else you'll get 0 as a result).
The second small mistake is that you cin>>num2 before your while-loop which will give the user the impression as if he multiplied e.g. 4 and 5, whereas he multiplied 1 and 5 because the 4 was overwritten by his second input to fix this you could either delete that line, or let the user enter product before the lopp and num2 in the loop body.

Hope this helped,
Michael
Thanks man
Topic archived. No new replies allowed.