Code Help

i need the code to have the user input a number and have it say an error if it is not positive. heres my code

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


#include <iostream>
#include <sstream>

using namespace std;

int Persistent(int value)
{

int counter = 0;

while (value > 10)
{
int newvalue = 1;
int tmpvalue = value;


while (tmpvalue > 0)
{	
// I get the last digit
int tmp = tmpvalue % 10;
newvalue *= tmp;
// last digit is cut
tmpvalue /= 10;
}
value = newvalue;
cout << "element: " << value << endl;
counter++;	
}

return counter;

}

int main()
{
    
    cout << "Please enter a Non - Negative Number";
    
int originalNumber = 715;
cout << "original number: " << originalNumber << endl;
cout << " number of element: " << Persistent(83);
return 0;
}
Use cin.

Also you can create an if/else to check for negativity or zero case:
1
2
3
4
5
6
7
// pseudocode
if number is negative
    do something
else if number is 0
    do another thing
else
    continue to main program

how would i implement it in code
Below is an example of the while loop use for validation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{
	int number{ 0 };

	cout << "Please enter a number (0-100)" << endl;
	cin >> number;

	while (number < 0 || number > 100) 
	{
		cout << "Your input " << number << " is incorrect" << endl;
		cout << "Try again" << endl;
		cout << "Please enter a number (0-100)" << endl;
		cin >> number;
	}

	return 0;

}


If you don't know how to use the while loop, then I recommend using the if statement. However, for the if statement, it will check for the wrong input and let the user know only once.
closed account (48T7M4Gy)
Large team of volunteers working on this project for OP all over the place

http://www.cplusplus.com/forum/general/177311/
http://www.cplusplus.com/forum/beginner/177290/
http://www.cplusplus.com/forum/beginner/177321/
so how will do code look with that
closed account (48T7M4Gy)
Even even Larger team of volunteers working on this project for OP all over the place

http://www.cplusplus.com/forum/general/177311/
http://www.cplusplus.com/forum/beginner/177290/
http://www.cplusplus.com/forum/beginner/177321/
http://www.cplusplus.com/forum/beginner/177330/
http://www.cplusplus.com/forum/beginner/177327/
@ kemort, thanks for the heads up!
@ OP, thanks for wasting my time.
closed account (48T7M4Gy)
@chicofeo ;)
Topic archived. No new replies allowed.