Product of only Positive Numbers/Loop and Continue Statements

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.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <conio.h> // For function getch()
#include <cstdlib>  // For several general-purpose functions
#include <fstream>  // For file handling
#include <iomanip>  // For formatted output
#include <iostream>  // For cin, cout, and system
#include <string>  // For string data type
using namespace std; // So "std::cout" may be abbreviated to "cout"

int main()
{
int num1; //user input variables
int num2;
int num3;
int num4;
int num5;
int negative = 1; //for negative numbers
int product = 1; //product output

cout << "Product of Positive Numbers Calculator" << endl; //Program Header
cout << "======================================" << endl;
cout << " " << endl; //Blank Line
cout << "Please enter 5 numbers: " << endl;

while (num1, num2, num3, num4, num5 > 0)
{
cout << "Enter numbers please." << endl;
cin >> num1;
cin >> num2;
cin >> num3;
cin >> num4;
cin >> num5;

if (num1 > 0)
product *= num1;
else
num1 = 1;
if (num2 > 0)
product *= num2;
else
num2 = 1;

if (num3 > 0)
product *= num3;
else
num3 = 1;

if (num4 > 0)
product *= num4;
else
num4 = 1;

if (num4 > 0)
product *= num4;
else
num4 = 1;
}

cout << "The product of the postive numbers are: " << product << endl;

return 0;
}


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];
  }


Last edited on
Some immediately obvious issues with this code:

24
25
while (num1, num2, num3, num4, num5 > 0)
{


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?
Topic archived. No new replies allowed.