-3

I am trying to get a divider in my program, I got it to output however I notice their is a gap on the top. For example my code outputs:

*******************************************************************************************

                                                  Broome
                                                  CST 113 Y01
                                                     LAB 
                                                  Stan
******************************************************************************************

I just want the code to output like this:

  *******************************************************************************************                   
                                                  Broome
                                                  CST 113 Y01
                                                     LAB 
                                                  Stan
******************************************************************************************

without that annoying gap. Here is my that outputs the divider:

//Output the divider to the screen
cout << setfill('*') << setw(SCREEN_WIDTH + 1) << " " << setfill(' ') 
     << endl;

// Output the course heading to the screen
cout << setw((SCREEN_WIDTH + 11) / 2) << COLLEGE << endl;
cout << setw((SCREEN_WIDTH + 11) / 2) << COURSE << endl;
cout << setw((SCREEN_WIDTH + 5) / 2) << LAB_NAME << endl;
cout << setw((SCREEN_WIDTH + 15) / 2) << PROGRAMMER_NAME << endl;

//Output the divider to the screen
cout << setfill('*') << setw(SCREEN_WIDTH + 1) << " " << setfill(' ') << endl;

I am sure it is something simple I am missing

sss34
  • 57
  • 9

1 Answers1

0

I duplicated your problem using MinGW and running in a 80 column Windows console with SCREEN_WIDTH set to 80. If the console is wider than SCREEN_WIDTH + 1, the problem is not exhibited.

Your code will print exactly SCREEN_WIDTH asterisks followed by a space. If your console is exactly SCREEN_WIDTH columns wide, that space will actually be printed on the next line (though you can't see it -- it's a space!). Even if you subtract one, the space will still be on the last column of the line, causing the cursor to drop a line. The endl will do that again.

I was able to get results very close to what you're looking for with this:

 cout << setfill('*') << setw(SCREEN_WIDTH) << "\n" << setfill(' ');

It's one asterisk short of a full line, but that's about the best you can do. If you know you will always run on the same console type with exactly SCREEN_WIDTH columns, you could use empty quotes instead of the "\n". But that's assuming a lot of the console.

Resulting code I used:

#include <iostream>
#include <iomanip>

using namespace std; // BLECH!!

int main()
{
    const auto SCREEN_WIDTH = 80;
    const auto COLLEGE = "Broome";
    const auto COURSE = "CST 113 Y01";
    const auto LAB_NAME = "LAB";
    const auto PROGRAMMER_NAME = "Stan";

    //Output the divider to the screen
    cout << setfill('*') << setw(SCREEN_WIDTH) << "\n" << setfill(' ');

    // Output the course heading to the screen
    cout << setw((SCREEN_WIDTH + 11) / 2) << COLLEGE << endl;
    cout << setw((SCREEN_WIDTH + 11) / 2) << COURSE << endl;
    cout << setw((SCREEN_WIDTH + 5) / 2) << LAB_NAME << endl;
    cout << setw((SCREEN_WIDTH + 15) / 2) << PROGRAMMER_NAME << endl;

    //Output the divider to the screen
    cout << setfill('*') << setw(SCREEN_WIDTH) << "\n" << setfill(' ');
}

Output:

*******************************************************************************
                                       Broome
                                  CST 113 Y01
                                       LAB
                                           Stan
*******************************************************************************
Fred Larson
  • 60,987
  • 18
  • 112
  • 174