scan input for specific number

I'm writing a program which ask for an input number range from 0 to 9999. If the number is within the range, the program will check if the number contain digit 1, 2 or 3, then output them according.

My question is how do I scan the input number for the digit 1, 2 and 3, and then output them accordingly? Thanks in advance.

eg if I enter "1234", the output will say "it contain 1, 2 and 3."
if I enter "987" it says "it doesn't contain 1, 2 or 3."
if I enter "9817" it says "it contain 1."
if I enter "9221" it says "it contain 1 and 2."

Here's my code so far
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
#include <iostream>

using namespace std;

int main()
{
    int number;
    for( ; ; )
   {
    	cout << "Enter the desired number:" << endl;
	    cin >> number;
	    
	    if (number < 9999 && number > 0)
                {
	        //if the number contain 1, 2 or 3.
                   if


	        //if the number doesn't contain 1,2 or 3.
                  else
                   cout << number << "does not contain the digit 1, digit 2 or digit 3." endl;
                }


	    else
	        {
                cout << "Invalid entry, pls re-enter number between 0 and 9999..." << endl;
                }
   }
   return 0;
}
You should look up how to extract specific digits from numbers. The math is very simple. Once you have that, just loop through extracting each digit and checking if it is 1, 2, or 3.
Last edited on
Thanks, I figured it out now
Topic archived. No new replies allowed.