Write a complete program that reads three integers from the console and outputs the number of distinct values entered. For example if the input is 34 55 23 then the output will be 3, since there are three distinct values . If the input is 42 78 42 then the output will 2 since there are two distinct values . Finally, if the input is 67 67 67 then the output will be 1 since there is just one distinct value .

Here is my source code:

#include<iostream>
using namespace std;

int main()
{
    int a, b, c, distinct;

    cin >> a >> b >> c;
    if(a != b && b != c && a != c)
    {
        distinct++;
        cout << distinct << endl;
    }
    else if((a != b && b != c && a == c) 
    || (a == b && b == c && a != c) 
    || (a != b && b == c && a != c) || (a == b && b != c && a != c))
    {
        distinct++;
        cout << distinct << endl;
    }
    else
    {
        cout << distinct << endl;
    }

    return 0;
}

Here is my input:
34 55 23

Here is my output:
1

Here is the expected output I want to get:
3

Is there anyway I can fix this?

Dani AI

Generated

The immediate problem in 's code is that distinct is used without being initialized, which is undefined behavior and explains the unexpected 1 output. The else if list is also overcomplicated and includes logically impossible tests (for example (a == b && b == c && a != c) can never be true). A compiler run with warnings enabled (for example g++ -Wall) will typically point out the uninitialized use.

A minimal, clear fix using simple logic for exactly three values:

#include <iostream>

int main() {
    int a, b, c;
    if (!(std::cin >> a >> b >> c)) return 0;

    int distinct;
    if (a == b && b == c) distinct = 1;
    else if (a == b || a == c || b == c) distinct = 2;
    else distinct = 3;

    std::cout << distinct << '\n';
    return 0;
}

An alternative that scales a bit better and avoids manual branching is to use a small container to hold unique values:

#include <iostream>
#include <set>

int main() {
    int a, b, c;
    if (!(std::cin >> a >> b >> c)) return 0;

    std::set<int> s = {a, b, c};
    std::cout << s.size() << '\n';
    return 0;
}

Both fixes avoid the uninitialized counter. For longer input lists the approach suggested by (sort + std::unique) or using an std::unordered_set is appropriate; for exactly three integers the branch version or a std::set is simpler and clearer.

Here is an example of how this can be achieved using std library.

#include <iostream>
#include <algorithm>
#include <vector>

int main() 
{
    std::vector<int> v{1,2,3,1,2,3,3,4,5,4,5,6,7};
    std::sort(v.begin(), v.end()); 
    auto last = std::unique(v.begin(), v.end());
    v.erase(last, v.end());
    for (const auto& i : v)
      std::cout << i << " ";
    std::cout << "\n";
}

You would of course have to get your input into a vector container and modify the output.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.