Hello, I just need a little help with my coding. I need to write a program that asks the user to input 5 numbers and prints out a product of only positive numbers from these 5 numbers. It also says to use for loop and continue statement to skip non‐positive numbers. My code so far is below.
Honestly, this is a homework assignment, so I don't really want the answer right off the bat. I need to learn this myself, any help would be greatly appreciated. Thank you so much!
Line 24: C++ does not support implied left hand side in conditionals. You must fully specify the conditions. The comma operator will cause only the last number to be compared to 0. See the explanation of the comma operator here: http://www.cplusplus.com/doc/tutorial/operators/
It also says to use for loop and continue statement to skip non‐positive numbers.
That implies an array rather than individual variables.
1 2 3 4 5 6 7 8 9
int num[5];
for (int i=0; i<5; i++)
{ cout << "Enter number " << i+1 << ": ";
cin >> num[i];
if (num[i] <= 0)
continue;
product *= num[i];
}
I'm guessing this is supposed to be checking that all of those variables are above 0? Or maybe that one of them is above 0? In either case, you can't construct your logical statement like that. The comma operator doesn't mean what you seem to think it means.
I recommend you read up on the logical operators || (for "or") and && (for "and"), and get a handle on how to construct compound logical comparisons.
Also, what do you think the values of those variables will be the first time that while statement checks them?