A for loop will not work in this situation because you don't know how many times you want the loop to run. The user decides that. But for learning purposes, if you knew the loop would only run 12 times, a simple for loop would look like this:
1 2 3 4
|
for(int i=0;i<12;i++){
//do stuff
}
| |
Lets break the header down.
1.) "int i=0" declare a variable i and set it equal to 0.
2.) "i<12" if i is less than 12 (if 0 is less than 12) continue to loop
3.) "i++" increment i by 1 each time the loop runs. Eventually i will become equal to 12 which will break the loop.
But again, that's just an example do NOT use that loop for your program. To do the desired result I would get rid of the for loop inside your while loop and check if the input is odd by using the modulus operator (%). For example:
1 2 3
|
if(input % 2 != 0){ //if you divide it by 2 and there is a remainder.
//input is odd
}
| |
I would then advise storing input in a variable like you have such as "product". So you would have a line like
|
product *= input //multiples then stores the product in product (updates the value)
| |
So each time the while loop runs it will first detect if input is odd. If input is odd, it will multiply that odd value by the product of previous odd values and continue until the user enters -999