Need help with output formatting in c++
Can anyone tell me methods to make c plus plus output formatted , and make it look attractive? I am particularly interested in functions that could help me get formatted output .heres a code i want to format . void student::display() { clrscr(); textcolor(GREEN); cout<<"\n\n\t\t"; cputs("MARK LIST"); cout<<"\n\t\t_________"; cout<<"\n\nNAME:"<<name; cout<<"\nSTANDARD:"<<std; cout<<"\nROLL NUMBER:"<<rolno; cout<<"\nPERCENTAGE:"<<per; cout<<"\nGRADE:"<<grade; } well it looks a mess when printed directly .So i need to know how i can make it look catchy(i used color function) and formatted(i didnt use any function yet).
So you want the documentation to be readable? CODE BLOCKS!
Okay, you want the output nice.
well in my lab exam the total mark is divided and there is separate mark for formatted output
yep @ParthKohli
Not good at C++. I know only some languages :-( I can read codes but not so good at writing them out, and I see that it's just a student's summary. :-|
nvm...anyway thanks for looking
@FirstFrostByte ,@hba can you please help ?
@hba
For aligning bits of text, you could try iomanip's setw function (see: http://www.cplusplus.com/reference/iomanip/)
@slotema that link is not working !
the closing ) was included in the URL. Try this one: http://www.cplusplus.com/reference/iomanip/
hmm...well can u help me formatting this particular part of code above?
Sure thing. Let's start from the longest 'label', which is "ROLL NUMBER:". Add a space for legibility and that's 13 characters wide. So we need to say in your program that all the labels should be 13 characters wide. That way, all the values will be aligned. Taking a small bit of your code as example: cout<<"\nSTANDARD:"<<std; cout<<"\nROLL NUMBER:"<<rolno; will become: cout<<"\n" << setw(13) << "STANDARD:"<<std; cout<<"\n" << setw(13) << "ROLL NUMBER:"<<rolno; What this will do is making sure that the space taken by both "STANDARD:" and "ROLL NUMBER:" is 13 characters long. If you try this for all couts, you will notice that the text is right-aligned. If you want to have the text left-aligned, you should add the following line before the first setw(): cout.setf(ios_base::left); setf() sets a number of flags ( http://www.cplusplus.com/reference/ios/ios_base/fmtflags/ ), like the alignment or how integers are shown (hexidecimal, decimal or octal) etc.
thanksa lot !! that was about formatting ..how to make it look catchy ? like i used color for heading
any other suggestion?
Perhaps giving the labels a different colour than the values. One thing that would be nice is displaying a failing grade in red and a passing grade in green.
NICE !!! does setw require extra header file ?
or is it included in iostream?
You'll need to #include <iomanip> Another thing is if you have any floating point numbers, try to get the dots aligned.
that using precision() ?
Precision is about the number of digits to display after the decimal point. I don't know if there is an easy way to align them, but you can split up the number (let's say 10.34) into a fractional part (.34) and an integer part (10). You can then use setw with right-alignment to print the integer part, followed by a period and than print the fractional part. Something like: cout << setw(5) << integer << "." << fraction;
whats the diff between setw and width functions ?
i read that width function can also be used to allign
I guess i am late :/
no u are not i have a bunch of doubts with me ! :)
The only difference between width and setw that I can see is the way they are used. Width, you have to use like `cout.width(x);`, while setw is used as `cout << setw(x);`. In my opinion, setf has the advantage of fitting in between strings (like `cout << "Hello, " << setw(x) << "World!";`. It is a bit more intuitive for me than the width alternative: cout << "Hello, "; cout.setw(x); cout << "World!";
(that `cout.setw(x);` should of course be `cout.width(x);`)
yaa i also noticed that ...
does the color function work for only one line?
because when i got output MARK LIST only had colour
I don't know. I'm not familiar with textcolor(). It can be due to the use of cputs().
hmm....is there a function by which we can change output font size?
Not that I know of. The command line usually has a fixed size for characters.
oh thanks a lot !! this discussion was really useful for me. do help me next day :) more doubts coming up as i am revising for my finals
Just put your question on OpenStudy. That way anyone can help you.
Join our real-time social learning platform and learn together with your friends!