Need a simple program code review please

Hello, I recently tried to code a program that displays if any digit in a given number in a (10000 to 99999 range) is equal to another one

(e.g: if the user inputs the number 12234, the display output wanted would be: The Digit 2 is repeated in this number)

I would like some tips to code a cleaner way to compare the digits of a given number together (line 56-82)
because if a number has the same digit 3 or 4 times, my outputs gives the name number multiple times.

Also, How would i code to test all of my digits equality conditions and would output a message( cout << "/n there is no digits that are equals"; ) only if there is NONE of my previous if conditions that are true. Instead of setting a variable called "digitcounter"

*Please note that i would like to keep my code with the same type of variables and c++ keywords as much as possible, because I am learning from a class.


its only been a week i started learning C++ go easy on me please

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  #include <iostream> // declaring ...	
using namespace std; // using the std input for cin & cout

int main()
{

	 unsigned int digitcounter{ 0 };// used to assign if at least one of our equality conditions is met
	 
	 unsigned int number{ 0 };
	 unsigned int digit1{ 71 };
	 unsigned int digit2{ 72 };
	 unsigned int digit3{ 73 };
	 unsigned int digit4{ 74 };
	 unsigned int digit5{ 75 };

	cout << " Please enter 5 digit number: "; // ask the user for a number

	cin >> number; // the user enters the number in input

	if (number > 99999 || number < 10000) // condition to make sure the user enter maximum 5 digits12
	{
		cout << "\n The number you intered is invalid for this program, use a number between 10000 and 99999";
		return 0;
	}
	else
	{

		
			digit1 = number % 10;    // assign digit1 to the right-most digit of the 5 digits integer
			number = number / 10;

			digit2 = number % 10;
			number = number / 10;

			digit3 = number % 10;
			number = number / 10;

			digit4 = number % 10;
			number = number / 10;

			digit5 = number % 10;
			number = number / 10;

		

		
		if (digit1 == digit2 || digit1 == digit3 || digit1 == digit4 || digit1 == digit5) // comparing all digits together to find equals digits
		{

		    cout << " the digit  " << digit1 << "  is repeated in this number\n"<<endl;
			digitcounter += 1;
		}


		if (digit2 == digit3 || digit2 == digit4 || digit2 == digit5)
		{
			 cout << "the digit  " << digit2 << " is repeated in this number\n"<<endl;
					digitcounter += 1; 
		}
		if (digit3 == digit4 || digit3 == digit5)
		{
			 cout << "the digit  " << digit3 << " is repeated in this number\n"<<endl;
				digitcounter += 1;
		}
		if (digit4 == digit5)
		{
			 cout <<"the digit  " << digit4 << " is repeated in this number\n"<<endl;
				digitcounter += 1;
		}
	if (digitcounter == 0) // when all of the line are false the digitcounter will equal to 0, meaning that theres no
	{

	 cout << "/n theres is no digits that are equals";
	}

	} // end of "else" brace line 28


} // end of main
Last edited on
> its only been a week i started learning C++ go easy on me please
It's nice to see a first post with a coherent question and some actual code - well done!.

So do you know about arrays yet?

> unsigned int digit1
> unsigned int digit2
Because whenever you start to put numeric suffixes on your variables, what you really want is an array.

1
2
3
4
5
        unsigned int digits[5];
        for ( int i = 0 ; i < 5 ; i++ ) {
            digits[i] = number % 10;
            number = number / 10;
        }


You can then use for loops to scan the array looking for duplicate numbers (hint: you might need a loop inside a loop).

Which in turn would help you to address your repeated duplicate question for when the user types in say 22222.

What salem c said.

Also for a beginner I have to say your code is well structured and annotated, keep that up, Looks really tidy!
I havent learned about array yet. I will update my code once I do. Id like to find the nested loop your talking about by myself before asking here :)

and yeah the best way for me not getting confused is documenting everything ! do!

for now i declare almost all of my variable as int because im only doing exercice returning a value atm. In this code I used <<unsigned int>> because I remember that my prof said using "unsigned" when we code for a program without negative number is optimal memory wise.

thank you very much salem c.
Last edited on
closed account (E0p9LyTq)
I havent learned about array yet.

When you learn about C-style strings, char arrays, or C++ strings, comparing digits will really be easy.
Topic archived. No new replies allowed.