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.
#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);
}
elseif(colour == 1)
{
red++;
}
elseif(colour == 2)
{
green++;
}
elseif(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";
}
elseif(green > red || green > blue)
{
std::cout<<"Green is more popular than red or blue.\n";
}
elseif(blue > green || blue > red)
{
std::cout<<"Blue is more popular than red or green.\n";
}
elseif(red == 5 && green == 5)
{
std::cout<<"Red and green are more popular than blue.\n";
}
elseif(red == 5 && blue == 5)
{
std::cout<<"Red and blue are more popular than green.\n";
}
elseif(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 ;)