Verification of code

hi can someone advise me on why my programme code cannot run properly. The program supposed to prompts the user to enter a number ranging from 0 to 9999. the programme must validate this range . The program checks if the input number contains the digit 1, digit 2 or digit 3.



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
#include <iostream>
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main ()
{
	char input[4];
	bool valCheck =true; //initialize as true first. If contain 1, 2 or 3 then will be set to false in the loop
	int number;
	bool getNumber;
	int o, t, h, th;
	
		cout << "Enter the desired number:\n";
		getNumber = true;
		
	while (getNumber == true)
	{
		cin	>> number;
		if ((number<0)||(number>9999)) //validate number is smaller than 0 or bigger than 9999 
		{  
			cout << "Invalid entry, pls re-enter number between 0 and 9999...\n";
			cout << "Enter the desired number:\n";
		}
		else
		{
			getNumber = false; //if number is within range, then set to false so that loop can end
			//initialize the array with values from the user entered number
		
		for (number >= 0 || number <= 9999);
				
				//to get all the digits separately
				o = number / 1 % 10;     //ones
				t = number / 10 % 10;	  //tens
				h = number / 100 % 10;    //hundreds
				th = number / 1000 % 10;  //thousands
				
				if (o == 1 || o == 2 || o == 3 || 
					t == 1 || t == 2 || t == 3 ||
					h == 1 || h == 2 || h == 3 ||
					th == 1 || th == 2 || th == 3)
			
				cout << "Entered number, " << number << ", contains the digit 1, 2 or 3.\n";
				}
				else
				{
				cout << "Entered number, " << number << ", does not contain the digit 1, 2 or 3.\n";
				}
		
}
		return 0;
}


Last edited on
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.

What does "code cannot run properly" mean? Does it delete your family photos? Does it crash your neighbor's phone? Does it install a virus created by the NSA? You need to be specific.
To make it easier for us, please do 2 things.

First, use code tags. If you click the Format button that says "<>" and then paste your code inside the tags, it will be easier for us to read and comment on. I think you can edit your previous post, highlight all of the code and click the button and it will take effect.

Second, the statement
why my programme code cannot run properly
is ambiguous. What is the program supposed to do, and what does it do instead? With more information, we can better diagnose the problem and point you towards a solution.
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
#include <iostream>

int main()
{
    bool has_1_2_or_3 = false ; // initialize as false first. If contain 1, 2 or 3 then will be set to true in the loop
    int number ; // number entered by the user
    bool get_a_new_number = true ;

    while( get_a_new_number )
    {
        std::cin >> number ;

        if ( number<0 || number>9999 ) //validate number is smaller than 0 or bigger than 9999
        {
            std::cout << "Invalid entry, pls re-enter number between 0 and 9999...\n";
            std::cout << "Enter the desired number:\n";
        }

        else
        {
            get_a_new_number = false; // if number is within range, then set to false so that loop can end

            std::cout << "number " << number ; // print out the entered number
                        // we do this early, because we want to modify the number later

            // if the number contains digits 1, 2 or 3
            while( number > 0 ) // while there are more digits left
            {
                const int last_digit = number % 10 ; // get the last digit

                if( last_digit > 0 && last_digit < 4 ) // if the last digit is 1, 2, or 3
                {
                    has_1_2_or_3 = true ; // indicate that we have found 1, 2 or 3
                    break ; // we have found what we were looking for; we can exit the loop
                }

                else // the last digit is not 1, 2, or 3
                {
                    number /= 10 ; // throw away the last digit and continue looking at remaining digits
                }
            }

            if( has_1_2_or_3 ) std::cout << " contains at least one of the digits 1, 2 or 3.\n";
            else std::cout << " does not contain the any of the digits 1, 2 or 3.\n";
        }
    }
}
Topic archived. No new replies allowed.