why doesn't my setw setfill bar graph work

why doesn't my setw setfill bar graph work on line 71-74

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
#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);        
        }
        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 the most popular colour.\n";
        }
        else if(green > red && green > blue)
        {
        std::cout<<"Green is the most popular colour.\n";
        }
        else if(blue > red && blue > green)
        {
        std::cout<<"Blue is the most popular colour.\n";
        }

        else if(red == green && red > blue)
        {
        std::cout<<"Red and green are the most popular colours.\n";
        }
        else if(red == blue && red > green)
        {
        std::cout<<"Red and blue are the most popular colours.\n";
        }
        else if(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;

}

If you want to use setw and setfill:
1
2
3
std::cout << "Red\t"   << std::setfill('*')   << std::setw(red + 1)    << ' ' << std::endl;
std::cout << "Green\t" << std::setfill('*')   << std::setw(green + 1)  << ' ' << std::endl;
std::cout << "Blue\t"  << std::setfill('*')   << std::setw(blue + 1)   << ' ' << 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);
}

Last edited on
Topic archived. No new replies allowed.