What am I doing wrong with these logic gates?

I am trying to write a program that takes a poll on favourite colours from 10 different users but i can't get the logic right for 5 each of 2. Please show me what I am doing wrong.

Thanks.

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


int main()
{

int colour;
int red = 0;
int green = 0;
int blue = 0;

    for (int i =0;i<10;i++)
    {
    std::cout <<"User " <<i+1<< " what is your favourite colour?\n";
    std::cout <<"(1) Red (2) Green (3) Blue\n";
    std::cin>>colour;
    
        
        if(colour == 0)
        {
        std::cout<<"0 entered. Program terminating.\n";
        exit(0);        
        }
        else if(colour == 1)         
        {
        red++;
        }
        else if(colour == 2)
        {
        green++;
        }
        else if(colour == 3) 
        {
        blue++; 
        }
    
    std::cout<<"Red = " <<red<< " Green = " <<green<< " Blue = "<<blue<<std::endl;
}

        if(red > green || red > blue)
        {
        std::cout<<"Red is more popular than green or blue.\n";
        }
        
        else if(green > red || green > blue)
        {
        std::cout<<"Green is more popular than red or blue.\n";
        }

        else if(blue > green || blue > red)
        {
        std::cout<<"Blue is more popular than red or green.\n";
        }


        else if(red == 5 && green == 5)
        {
        std::cout<<"Red and green are more popular than blue.\n";
        }

        else if(red == 5 && blue == 5)
        {
        std::cout<<"Red and blue are more popular than green.\n";
        }

        else if(green == 5 && blue == 5)
        {
        std::cout<<"Green and blue are more popular than red.\n";
        }


        else
        {
        std::cout<<"No output.\n";
        }

}

(red > green || red > blue) should be (red > green && red > blue)
Red must be greater than green AND red must be greater than blue.

Next try doing something like this:
Compare all the values to find highest
-> If the highest is equal to second highest, print highest and second highest as being the most popular else print just the highest as being the most popular (we're ignoring the third number as all three numbers cannot be equal when there are 10 surveys).

Also, what happens if I type '50'? Consider that too ;)
Can you show me the correct logic syntax for 2 values being equal and both greater than the third value? Thanks.
the following must be true:

Value1 equals Value2
AND
Value1 is greater than Value3

Last edited on
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main(void) {
     int a=8, b=8, c=0;
     std::cout << "Enter an integer: ";
     std::cin >> c;
     bool z=(a==b && a>c);
     std::cout << z << std::endl;
return 0;
}

(obviously if a and b are equal, then you only have to test if one of them is greater than c, to know if both a and b are greater than c or not)
Last edited on
Topic archived. No new replies allowed.