Set Width between output for variable length names
Hello experts!
After some reading i have managed (for the most part) to convert from Microsoft Visual Studio C++ 6.0 to Ubuntu's Terminal using the G++ standard libraries / functions to compile and run my code.
I have a register system that i am creating to find out how my coding needs to be adapted to the Linux standard coding (wanting to ditch Microsoft products from here on in!)
I have the following problem in my system and i think i have the solution but no way to implement it.
i have a function called takeregister() this loads student names from a static array called init() and loops through the names stored in the init() array and asks if that student is present. Pretty straight forward.
once the array has been processed (all students have been asked if they are present, and an input stored) the program then shows the results.
HOWEVER...
Being a perfectionist i don't like having this as the output:
what im after is output like this:
XXXXXXXX XXXXXXXXX attendences: 1 absences: 4
XXXXX XXXXXXXX attendences: 2 absences: 3
XXX XXXXXXX attendences: 3 absences: 2
XXX (firstname) XXXXXX (surname) <- both part of a statically defined array of characters:
the output im currently getting is:
XXXXX XXXX attendences: 1 absences: 4
XXXXXXX XXXX attendences: 1 absences: 4
etc etc...
int init(char szstudentnames[maxstudents][30])
{
system("clear");
// initiate static 2D array of characters of student names
cout << " " << left << setw(30) << name << szstudentnames[i]
<< " Attendences: " << right << setw(2) << nattendence[i]
<< " Absences: " << right << setw(2) << nabsence[i];
Note that the width for the names is increased (the examples you gave contained names longer than 20 chars).
0
An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.
One of a set of tools we're offering as a way of saying thank you for being a part of the community.
how easy would it be to store the attendance of each student into a text file, then import it before the register is taken again?
eg:
register is taken for 2 lessons: everyone is present for both (22 student all with Y)
close program.
reload program. find text file and load attendance figures from previous registers into the buffer.
(22 students for 2 classes all present)
then take the new register.
Sorry, jaime_olivares. It was not my intention to get these points.
@ seraph_matrix_631 : jaime_olivares gave you the correct answer to use setw. I merely expanded on it to fix the alignment and other issues. If you want, you can re-open the question, and award all or some of the points to jaime_olivares.
>> how easy would it be to store the attendance of each student into a text file, then import it before the register is taken again?
Not that difficult. Take a look at this tutorial on file I/O in C++ :
i found that link earlier but thanks.
been playing with m coding for the program most of this afternoon trying to find a way now to check for numeric input before continuing a body of code.
i'll get there soon! :P
i gave u the points infinity08 because u most correctly answered my question. if u look at the title i did mention "set width" and i knew of its function, not wasnt sure how to apply the function.
thanks to all for your help :-)
0
Question has a verified solution.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
cout << setw(40) << yourstr << endl;