making header function

This is a discussion on making header function within the C++ Programming forums, part of the General Programming Boards category; I am trying to create a simple function that I can use for the header of my programs which will ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    10

    making header function

    I am trying to create a simple function that I can use for the header of my programs which will have my name, the date, and the program name. My name will always be the same and the date will always be 00-00-00 but the problem is if my program name is different the nice little box I have around my header will be offset depending on how many characters that line is. I am not sure if I explained that well but here is the code:

    Code:
    #include <string>
    #include <iomanip>
    using namespace std;
    
    void printHeader(string prog, string date){
      int len1 = prog.length();
      int diff = 69 - len1;
      int space = diff/2;
    
    
    
      cout<<"\n\n";
      cout<<"+-------------------------------------------------------------------+\n";          
      cout<<"| = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = |\n"; 
      cout<<"|{}.-------------------------------------------------------------.{}|\n";      
      cout<<"|: |                                                             | :|\n";   
      cout<<"| :|=MyNameHere      ||  Created: "<<date<<" || Modified: 00-00-00=|: |\n";       
      cout<<"|: |                                                             | :|\n";
      cout<<"| :|"<<setw(space)<<prog<<setw(space)<<" |: |\n";
      cout<<"|{}.-------------------------------------------------------------.{}|\n";
      cout<<"| = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = |\n";
      cout<<"+-------------------------------------------------------------------+\n\n\n";
      }
    basically I am trying to create a certain amount of spaces on each side of the 'prog' variable depending on the size of the variable so that my box will line up. This code here does not work but I think I am on the right track, can anyone help?

    thanks

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Well, I would start by finding the number of spaces you could use if the variable wasn't even there (lets say its 50). Then, from that number I would take the ammount of characters in the prog variable (using strlen()). Then I would Half that number, and print that many spaces, followed by the prog variable, then that many spaces again.

    eg:
    Code:
    #include <string.h>
    #include <iostream.h>
    
    int main()
    {
       cout all the normal stuff up to that line...
    
       int SizeOfProg = strlen(prog);
       int NumOfSpaces = (50 - SizeOfProg) / 2; // 50 - Length of prog, halved
       cout<<"| :|"<<printSpaces(NumOfSpaces)<<prog<<printSpaces(NumOfSpaces)<<" |: |\n";
    
       cout the rest of the stuff
       
    }
    
    
    printSpaces(int NumSpaces)
    {
       int X = 0;
       while(X != NumSpaces)
       {
          cout << ' ';
          X++;
       }
    }
    This code might be wrong but you can probably see what I mean.

    ~ Paul

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    10
    okay I got as far as this:

    Code:
    #include <string>
    #include <iomanip>
    using namespace std;
    
    void printHeader(string prog, string date){
     
    
    
    
      cout<<"\n\n";
      cout<<"+-------------------------------------------------------------------+\n";          
      cout<<"| = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = |\n"; 
      cout<<"|{}.-------------------------------------------------------------.{}|\n";      
      cout<<"|: |                                                             | :|\n";   
      cout<<"| :|=MyNameHere      ||  Created: "<<date<<" || Modified: 00-00-00=|: |\n";       
      cout<<"|: |                                                             | :|\n";
       int SizeOfProg = prog.length();
       int NumSpaces = (50 - SizeOfProg) / 2; // 50 - Length of prog, halved
       cout<<"| :|"<<printSpaces(NumSpaces)<<prog<<printSpaces(NumSpaces)<<" |: |\n";
      cout<<"|{}.-------------------------------------------------------------.{}|\n";
      cout<<"| = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = |\n";
      cout<<"+-------------------------------------------------------------------+\n\n\n";
      }
    
    void printSpaces(int NumSpaces)
    {
       int X = 0;
       while(X != NumSpaces)
       {
          cout << ' ';
          X++;
       }
    }
    the only thing i changed was strlen(prog) to prog.length() because it didnt recognize the other function. The error I am getting is: implicit declaration of function 'int printSpaces(...)' As I am very new to using functions (just started this in class yesterday) I have no idea what this means, any help much appreciated.

  4. #4
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Yeh, You'll need to either put
    Code:
    void printSpaces(int NumSpaces);
    at the top of your code
    
    --or--
    the whole printSpaces function just before your printHeader function
    You got this error because the printSpaces function had not been declared before you tried to use it.

    ~ Paul

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    10
    no luck i tried both things but it just gave me a whole lot of errors, to many to post. Any other suggestions? I put everything back the way it was so I am still just getting the same error as before.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,699
    This will do it:
    Code:
    void printHeader(string prog, string date){
      const int WIDTH = 60;
      int w1 = (WIDTH/2) + prog.length();
      int w2 = WIDTH - w1;
    
      cout<<"\n\n";
      cout<<"+-------------------------------------------------------------------+\n";          
      cout<<"| = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = |\n"; 
      cout<<"|{}.-------------------------------------------------------------.{}|\n";      
      cout<<"|: |                                                             | :|\n";   
      cout<<"| :|=MyNameHere      ||  Created: "<<date<<" || Modified: 00-00-00=|: |\n";       
      cout<<"|: |                                                             | :|\n";
      cout<<"| :|"<<setw(w1)<<prog<<setw(w2)<<" "<<" |: |\n";                       
      cout<<"|{}.-------------------------------------------------------------.{}|\n";
      cout<<"| = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = |\n";
      cout<<"+-------------------------------------------------------------------+\n\n\n";
    }
    gg

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    10
    still no dice I copied and pasted your exact code. No errors this time everything compiled fine but here is my output:

    Code:
    +-------------------------------------------------------------------+
    | = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = |
    |{}.-------------------------------------------------------------.{}|
    |: |                                                             | :|
    | :|=MyNameHere      ||  Created: 03-06-03 || Modified: 00-00-00=|: |
    |: |                                                             | :|
    | :|12345678                       |: |
    |{}.-------------------------------------------------------------.{}|
    | = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = |
    +-------------------------------------------------------------------+
    
    
    
    
    +-------------------------------------------------------------------+
    | = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = |
    +-------------------------------------------------------------------+
    its something with the first setw cause this happened with some code that i tried to. Any more suggestions?
    Also thanks to everyone who has replied thus far.

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    10
    HAHA VICTORY!
    I had to switch around where the prog.length was and subtract insteda of add it. Its not centered but it looks fine to me. Here is the final code:

    Code:
    void printHeader(string prog, string date){
      const int WIDTH = 60;
      int w1 = (WIDTH/2);
      int w2 = WIDTH - w1 - prog.length();
    
      cout<<"\n\n";
      cout<<"+-------------------------------------------------------------------+\n";          
      cout<<"| = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = |\n"; 
      cout<<"|{}.-------------------------------------------------------------.{}|\n";      
      cout<<"|: |                                                             | :|\n";   
      cout<<"| :|=MyNameHere      ||  Created: "<<date<<" || Modified: 00-00-00=|: |\n";       
      cout<<"|: |                                                             | :|\n";
      cout<<"| :|"<<setw(w1)<<""<<prog<<setw(w2)<<" "<<" |: |\n";                       
      cout<<"|{}.-------------------------------------------------------------.{}|\n";
      cout<<"| = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = |\n";
      cout<<"+-------------------------------------------------------------------+\n\n\n";
    
      cout<<"w1 is "<<w1<<endl;
      cout<<"w2 is "<<w2<<endl;
    
    }
    the couts at the end were just to help me figure out what was going wrong. Thanks again to everyone.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,699
    You can do true centering by understanding whats going on.
    -There are 61 spaces between the bars ("|<61sp>|"), since the back end of the bar is printed as " |: |\n", we can subtract a space.
    -The first calculation for w1 sets up a padding for printing "", which does NOT includes the string, so the start of the next output will be the true center, prog
    -The second calculation figures the rest of the padding needed to bring it out to WIDTH
    -Remember that when you setup a setw(), the next thing printed is right-justified within that field.

    Here's a better way, that truely centers:
    Code:
      //width between "|" and " |"  
      const int WIDTH = 60;
      
      //length of first half of prog
      int h = prog.length() / 2;
    
      //padding to print prog in center
      int w1 = (WIDTH / 2) + h;
    
      //padding to bring line out to WIDTH
      int w2 = (WIDTH / 2) - h;
      ...
        cout<<"| :|" << setw(w1) << prog << setw(w2) << " " << " |: |\n";                       
      ...

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21