Determine the number of equal integers of three entered

Hello,
I have to make a program which has to determine how many of the 3 entered numbers are equal.
The program outputs one of the numbers: 3 (if all numbers are the same), 2 (if the two numbers are the same) or 0 (if all the numbers are different).

The code brings me and 3 and 2. How can i fix it?
That's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include<iostream>
#include<cmath>
using namespace std;
int main(){
	
	{
	cout<<"Enter 3 numbers: " <<endl;
	int num1, num2, num3;
	cin>>num1;
	cin>>num2;
	cin>>num3;
	
	if ( num1=num3!=num2 || (num2=num3)!=num1 || (num1=num2)!=num3) {
		cout<<2<<endl;
	} 
	if (num1=num2=num3){
		cout<<3<<endl;
		}
	if ((num1!=num2) || (num2!=num3) || (num3!=num1)){
	
		cout<<0<<endl;
	}
Last edited on
You are using assignment operator = here
(which assigns one value with another):
if ( num1=num3!=num2 || (num2=num3)!=num1 || (num1=num2)!=num3)

To compare numbers you need to use comparison operator ==

1
2
3
4
5
if (num1 == num2 || num1 == num3 || num2 == num3)
      // it's 2 same numbers
else if (num1 == num2 == num3)
      // it's 3 same numbers
else // no same number 
Last edited on
Hello Chad9,

In addition to what malibor has said.

Your code could use some blank lines.
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
#include<iostream>
#include<cmath>

using namespace std;

int main()
{  // <--- No closing }.

	{  // <--- No closing } and not needed.

		cout << "Enter 3 numbers: " << endl;

		int num1, num2, num3;

		cin >> num1;
		cin >> num2;
		cin >> num3;

		if (num1 = num3 != num2 || (num2 = num3) != num1 || (num1 = num2) != num3)
		{
			cout << 2 << endl;
		}

		if (num1 = num2 = num3)
		{
			cout << 3 << endl;
		}

		if ((num1 != num2) || (num2 != num3) || (num3 != num1))
		{
			cout << 0 << endl;
		}

Also changing the style of the {}s makes it easier to see what is missing. You can use the style that you have learned, but the idea here is to make it easy to read and follow.

In the second if statement you are setting num2 equal to num3 then setting num1 equal to num2. As long as this is not (0) zero this is considered true. You need to compare num1 to num2 && num2 to num3. If both sides are true then all 3 numbers are equal.

In the third if statement using && tends to work better with the (!=).

In the first if statement the whole logic seems wrong. And it gives a warning when compiled. Still working on that part.

When you write your program compile often and fix the warnings and errors as you go. That way it is not so big at the end.

Andy
1
2
3
4
5
6
7
8
9
#include<iostream>
using namespace std;
int main()
{
   cout<<"Enter 3 numbers:\n";
   int num1, num2, num3;
   cin >> num1 >> num2 >> num3;
   cout << 2 * ( ( num1 == num2 ) + ( num2 == num3 ) + ( num3 == num1 ) ) - 3 * ( num1 == num2 && num2 == num3 );
}
You can simplify the comparisons by checking for all 3 equal first, then 2:
1
2
3
4
5
6
7
if (num1 == num2 && num2 == num3) {
    cout << 3 << 'n';
} else (num1 == num2 || num1 == num3 || num2 == num3) {
    cout << 2 << '\n';
} else {
    cout << 0 << '\n';
}
Hello Chad9,

Sorry I was delayed for awhile.

Taking into account everything that has been said I came up with this:
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
#include <iostream>
#include <limits>

using namespace std;

int main()
{
	int num1{}, num2{}, num3{};  // <--- Always a good idea to initialize your variables.

	cout << "Enter 3 numbers: ";
	cin >> num1 >> num2 >> num3;

	if (num1 == num2 && num2 == num3)  // <--- Checks if all 3 are the same.
	{
		cout << "\n All three numbers are the same.\n";
	}
	else if ((num1 == num2 || num1 == num3) || num2 == num3)  // <--- Checks if 2 are the same.
	{
		cout << "\n Two numbers are the same.\n";
	}

	else if ((num1 != num2) && (num2 != num3) && (num3 != num1))  // <--- Checks if none are the same.
	{
		cout << "\n No numbers are the same.\n";
	}


	// A fair C++ replacement for "system("pause")". Or a way to pause the program.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;  // <--- Not required, but makes a good break point.
}

For line 2. I do not know why you think you need "cmath", but you do not. There is nothing in your program that used anything from "cmath". For line 30 I changed this to "limits".

Line 11 does the same as what you have, just shorter.

Lines 13 - 25 correct some of the problems you first started with. I would have said that sometimes the order in which you code the if statements makes a difference, but dhayden's point is much simpler.

The stuff at the end of "main" you can keep or delete. I use it mostly for debugging the program, but it could be useful somewhere else.

Andy
Topic archived. No new replies allowed.