For loop multiplication

Hey guys,
I'm trying to write a program where the user inputs 8 numbers and the program, using a FOR LOOP, multiplies those 8 numbers and displays the result. I appreciate any feedback. **This code is very buggy and doesnt get me anywhere near where I want it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
using namespace std;

int main(){
int num0,num1, mult=0,i;
 cout << "Enter 8 integers." << endl;
 cin >> num0>>num1;
  for (i=0;i<8;i++)
  {

mult=mult+num0;


  }

cout << "The product of the 8 numbers is : " <<mult<< endl;


    return 0;
}
Put the cin >> num inside the for loop.
Put mult*=num inside the for loop.
And of course initialize mult=1 before the for loop.
I'm getting an infinite number of inputs for the user instead of only 8. Do you mind rewriting my code to show me what you mean?
Thanks.
closed account (48T7M4Gy)
Why don't you do it and if you are still having problems, post what you have done with a copy of the output along with a short explanation of why that's not what you expected.
I have done it multiple times. As of now, I'm not even getting a terminal output.
CODE:

#include<iostream>
using namespace std;

int main(){
int num0,num1,num2,num3,num4,num5,num6,num7,mult=1,i;
for (i=1;i<9;i++){

cout << "Enter 8 integers." << endl;

cin >>num0>>num1>>num2>>num3>>num4>>num5>>num6>>num7;

mult=*num0*num1*num2*num3*num4*num5*num6*num7;


}

cout << "The product of the 8 numbers is : " <<mult<< endl;







return 0;
}
Do you really mean to ask the user to enter 8 numbers 8 different times? You'll get no output until you've entered 64 numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// http://ideone.com/0eFXmq
#include<iostream>

int main() {
    const unsigned limit = 8;

    std::cout << "Enter " << limit << " integers.\n";

    int product = 1;
    for (unsigned i = 0; i < limit; ++i)
    {
        int value;
        std::cin >> value;
        product *= value;
    }

    std::cout << "The product of those " << limit << " numbers is " << product << '\n';
}


Take note of what is inside the loop and what is outside the loop.
closed account (48T7M4Gy)
@OP Well that is certainly one way of doing it. But you don't need the for loop. And you should initialise (set the start value) of any variables you have.

The for loop you had originally meant you were asking for the variables 8 times. Also starting mult =*num0 etc is the wrong way around *= as above, but even that is slightly off so you would have been getting a ridiculously large answer.

So, sticking to your original you needed something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
using namespace std;

int main() {
	int num0 = 0, num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0, num6 = 0, num7 = 0, mult = 1;

	cout << "Enter 8 integers." << endl;
	cin >> num0 >> num1 >> num2 >> num3 >> num4 >> num5 >> num6 >> num7;
	mult = num0*num1*num2*num3*num4*num5*num6*num7;

	cout << "The product of the 8 numbers is : " << mult << endl;

	return 0;
}


( The for loop makes it much easier. All you need to do is imagine if you had 1000 numbers. )
Thanks so much for the help mates!
Topic archived. No new replies allowed.