#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
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 the most popular colour.\n";
}
elseif(green > red && green > blue)
{
std::cout<<"Green is the most popular colour.\n";
}
elseif(blue > red && blue > green)
{
std::cout<<"Blue is the most popular colour.\n";
}
elseif(red == green && red > blue)
{
std::cout<<"Red and green are the most popular colours.\n";
}
elseif(red == blue && red > green)
{
std::cout<<"Red and blue are the most popular colours.\n";
}
elseif(green == blue && green > red)
{
std::cout<<"Green and blue are the most popular colours.\n";
}
else
{
std::cout<<"No output.\n";
}
//bar graph
std::cout<<"Red\t"<< std::setw(red) << std::setfill('*') <<std::endl;
std::cout<<"Green\t"<< std::setw(green) << std::setfill('*') <<std::endl;
std::cout<<"Blue\t"<< std::setw(blue) << std::setfill('*') <<std::endl;
}
setfill just sets what the remaining field must be filled with. setfill doesn't count as output and you need to output something if you're gonna use setw. I used ' '.
You only have to set the fill character once, and you probably want to reset afterwards it to what it was before. And you can use the newline character as the thing printed.
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include <iomanip>
int main() {
using std::cout; using std::setw;
int red = 10, green = 20, blue = 15;
auto savefill = cout.fill('*');
cout << "Red\t" << setw(red + 1) << '\n';
cout << "Green\t" << setw(green + 1) << '\n';
cout << "Blue\t" << setw(blue + 1) << '\n';
cout.fill(savefill);
}