Product of only Positive Numbers

I need to write a program that asks a user to input 5 numbers and prints out a product of only positive
 numbers. Can anyone revise my code? I can't envision how I would write the variables instead of (num1, num2, num3..... > 0). I have to also use for loop and continue statement to skip non‐positive numbers. 
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;
}
Unless you have actually been told to loop this repeatedly you don't need your outer while loop. Remove lines 24, 25 and 56 from the above.

Lines 52 and 53 are wrong: presumably you meant num5, not num4.



Less vital, but just suggestions ...

Most of your headers aren't used. Remove them.

else statements setting the numbers to 1 aren't necessary (if you are computing the product in bits). You could remove lines 35/36, 39/40, 44/45; 49/50, 54/55 unless you are planning to do something else in the future with these variables.

negative isn't used; remove line 16.

You could replace line 27 by a single cin statement (with repeated >> ). Alternatively, (particularly if you took up the array suggestion below) you could cumulatively update the product as you put the numbers in.

Since you are doing essentially the same operation on each number (and the number of numbers (!) might be different from 5 in the future) it would all be a lot tidier putting your input numbers in an array.
Topic archived. No new replies allowed.