Box around text using iomanip

Hi all, I have to display final result of my program in a centre and with a box around it. to Do that I was given notes on <iomanip>. I am complitely new to c++ and I never actually used iomanip, so this is what I came up with:

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
 #include<iostream>
#include<iomanip>
using namespace std;
int main (){
    string name;
    float score;

    cout << "\n\n\tenter name: ";
    cin >> name;
    cout << "\n\n\tenter score: ";
    cin >> score;
    cout << "\n\n" << setfill (' ') << setw (29) << " " << setfill ('-')
         << setw(23) << "-";
    cout<<"\n\n"<< setfill (' ') << setw(51) << "And the winner is\n\n" 
        << setw((80+name.length())/2)
        << name << "\n\n"<< setw(42) << setiosflags (ios::fixed) 
        << setprecision (2) << score;
    cout << "\n\n" << setfill (' ') << setw (29) << " " << setfill ('-') 
         << setw(23) << "-";
    cout << "\n\n";


return 0;
}
 


but how am I supposed to put a vertical line?
if my code completely stupid don't be afraid to say so...
but how am I supposed to put a vertical line?
Use '|' character.

If you want to be fancy, look at the extended ASCII table, symbols 179-218: http://www.asciitable.com/
Last edited on
MiiNiiPaa helpful as usual:)
So does it mean my sad little attempt is not so bad?
Well, it works. Another approach to outputting 23 dashes (or anything else) is to use string:
1
2
3
//call string constructor
//     How many elements ↓    ↓ Value of each element
std::cout << std::string(23, '-');
that is so much better
after setfill if I want to cancel it I setfill(' ')
is there something like cancel setfill comand?
this is very helpfull, thank you. After I setfill something and i want to cancel it I setfill(' ') is there any cancel stefill code?
Well, setting fill character back to space will work. You can get previous fill character this way:
1
2
3
char fill_char = std::cout.fill('-');
//set fill character to dash and store previous one in fill_char variable
//You can use it to restore fill character later 

Additionally if you need to cancel more than one manipulator, you can save stream format state and then restore it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <iomanip>

int main()
{
    //Create and save state
    std::ios  state(nullptr);
    state.copyfmt(std::cout);
    
    std::cout << std::setfill('-') << std::setw(20) << 'a' << '\n';
    
    //Restore state
    std::cout.copyfmt(state);
    
    std::cout << std::setw(20) << 'a' << '\n';
}
-------------------a
                   a

I got this for now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
   string name;
    float score;
    unsigned short l = name.length();

    cout << "\n\n\tenter name: ";
    cin >> name;
    cout << "\n\n\tenter score: ";
    cin >> score;
    cout << setw(40) << "|";
    cout << "\n\n" << setfill (' ') << setw (26) << " " << (char) 201 << string (22, (char)205) << (char) 187 << endl;
    cout << setfill (' ') << setw(27) << (char) 186 << setfill(' ') << setw(23) << (char) 186 << endl;
    cout<< setfill (' ') << setw(27) << (char) 186 << setfill (' ') << setw(20) << "And the winner is "
        << setw(3) << (char) 186 << endl;
    cout <<setfill (' ') << setw(27) << (char) 186 << setfill(' ') << setw(23) << (char) 186 << endl;
    cout<< setfill (' ') << setw(27) << (char) 186 << setw((80+l)/2-24) << name <<setw(7-l) << (char) 186 << endl;
    cout << setfill (' ') << setw(27) << (char) 186 << setfill(' ') << setw(23) << (char) 186 << endl;
    cout<< setfill (' ') << setw(27) << (char) 186 << setw(12) << setiosflags (ios::fixed) << setprecision (2)
         << score << setw(11)<< (char) 186 <<endl;
    cout << setfill (' ') << setw(27) << (char) 186 << setfill(' ') << setw(23) << (char) 186 << endl;
    cout << setfill (' ') << setw (26) << " " << (char) 200 <<string (22, (char) 205) << (char) 188
         << "\n\n\n\n";


my name is not in the centre and to be honest the whole box is a bit off as well. I didn't use the stream format state 'cos I don't fully understand it. I might try it later when i get my box and all centralized.
Any suggestions welcome..
Topic archived. No new replies allowed.