Solved

Set Width between output for variable length names

Posted on 2008-06-21
6
807 Views
Last Modified: 2013-11-15
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:

 * * * * * * * * * * * * * * * * * * *
 *   Student Attendences    *
 * * * * * * * * * * * * * * * * * * *
name 1      Attendences:  3     Absences:   2
 name 2      Attendences:  1     Absences:   4
 name 3      Attendences:  4     Absences:   1
 ....

i would prefer the output to be:

 * * * * * * * * * * * * * * * * * * *
 *   Student Attendences    *
 * * * * * * * * * * * * * * * * * * *
 Ben Simmons              Attendences:  3     Absences:   2
 Simon Smith                Attendences:  1     Absences:   4
 Hannah Stockholm      Attendences:  4     Absences:   1


code used so far:
cout << endl;
     cout << "  * * * * * * * * * * * * * * " << endl;
     cout << "  *   Student  Attendence   * " << endl;
     cout << "  * * * * * * * * * * * * * * " << endl;

     cout << endl;
   
     for (i=0; i<maxstudents; i++)

     {

        cout << szstudentnames[i] << "     " << "Attendences: " << nattendence[i] << "  " << "Absences: " << nabsence[i] << endl;

     }       

****************************
i think i need to:
take szStudentNames[i] length - maxlimit to give the differential

then i would need to  cout << szStudentNames[i] << differential << "Attendence: " <<  etc etc...

or do you have a better solution?
bearing in mind in a novice at C++
 

 

NOTE: source code file edited to remove personal data.

 

modus_operandi, EE Moderator

Open in new window

register-1-.txt
0
Comment
Question by:seraph_matrix_631
  • 3
  • 2
6 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21839454
try to use the setw() formatter function before every string you want to fix, like:

cout << setw(40) <<  yourstr << endl;
0
 
LVL 1

Author Comment

by:seraph_matrix_631
ID: 21840677
i have been looking at setw ...

i have used the following code (was all on one line but it didnt work so had to seperate it line by line for it to show up.

however with the code below im still getting varied widths and the names are all alligned to the RIGHT.


 for (i=0; i<maxstudents; i++)

     {

        cout << " ";
        cout << setw (20);  // set 20 characters for name
        cout << szstudentnames[i];
        cout << "     ";  
        cout << "Attendences: " << nattendence[i];
        cout << "     ";  
        cout << "Absences: " << nabsence[i] << endl;

     }       


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

   strcpy(szstudentnames[0], "name 0");

   strcpy(szstudentnames[1], "name 1");

   strcpy(szstudentnames[2], "name 2");

   strcpy(szstudentnames[3], "name 3");
   strcpy(szstudentnames[4], "name 4");

   strcpy(szstudentnames[5], "name 5");

   strcpy(szstudentnames[6], "name 6");

   strcpy(szstudentnames[7], "name 7");

   strcpy(szstudentnames[8], "name 8");

return 0;
}
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 125 total points
ID: 21840717
Something like :

        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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 1

Author Comment

by:seraph_matrix_631
ID: 21840876
cool!
brilliant thank you. works a charm

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.

then append it to the text file and save?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21842135
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++ :

        http://www.cplusplus.com/doc/tutorial/files.html
0
 
LVL 1

Author Comment

by:seraph_matrix_631
ID: 21842241
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

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Linux SSH Error 9 91
Need some help with Microsoft Visual Studio C++ 2003 5 51
trigs fail! I thought I knew how to do trignometry 3 56
What is sub-make ? 2 39
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now