Display the product of positive integers using functions.

Hi! I challenged myself to do another task but I was stuck again... I was tasked to find the product of positive integers using functions. The code will stop receiving answers when the user inputs either a zero or a negative integer. The code will then find the product of the entered numbers and will display the product. I got almost everything but the output wasn't as it should be.

We were given specific functions we can use. We can also add or modify them.
int accept_number()
bool ispositive(int)
int product(int, int)
void display(int)

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
  #include <iostream>
using namespace std;

int accept_number(); 
bool ispositive(int); 
int product(int, int); 
void display(int);

int main () {
	
	int enter, pos;
	
	enter = accept_number();
	
	if (ispositive(enter)) {
		cin>>pos;
		main();
	}
	else {
		return 0;
	}
		product (enter, pos);
		display (product(enter, pos));
	
}

int accept_number() {
	int x;
	cout<<"Enter a number: ";
	cin>>x;
	return x;
}

bool ispositive (int a) {
	int pos;
	if (a>0) {
		return true;
	}
	else 
		return false;
}

int product (int m, int n) {
	int prod;
	
	prod = m*n;
	return prod;
}

void display (int ans) {
	cout<<"The product is: "<<ans;
}


Here's what the output should look like:

Ex. Output 1:
1
2
3
4
5
6
Enter a number: 6
Enter a number: 3
Enter a number: 1
Enter a number: -2

The product is 18. 


Ex. Output 2:
1
2
3
4
5
6
Enter a number: 7
Enter a number: 2
Enter a number: 6
Enter a number: 0

The product is 84.


Ex. Output 3
1
2
3
Enter a number: -5

The product is 0.
You are calling main() recursively which you shouldn't do! You should use a loop.

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
#include <iostream>
using namespace std;

int accept_number();
bool ispositive(int);
int product(int, int);
void display(int);

int main() {
	int prod {};

	for (int enter {}, prev {1}; ispositive(enter = accept_number()); prod = product(enter, prev), prev = prod);

	display(prod);
}

int accept_number() {
	int x {};

	cout << "Enter a number: ";
	cin >> x;

	return x;
}

bool ispositive(int a) {
	return a > 0;
}

int product(int m, int n) {
	return m * n;
}

void display(int ans) {
	cout << "\nThe product is: " << ans << '\n';
}


Wow! Thank you so much! It works perfectly! <3<3
Topic archived. No new replies allowed.