Been at this for hours and cannot figure out how to get this the way I want.

I need it to center the text in the console output like this...

*******************************************
            ABC Industries
                Report
*******************************************

instead its coming out like this..

    **********************************
ABC Industries
Report
    **********************************

Here is what I got so far, any help will be gladly appreciated.

#include <string>
#include <iomanip>
#include <iostream>
#include <windows.h>
#include <cctype>
using namespace std;

class Heading
{
private:
    string companyName;
    string reportName;
public:
    Heading();
    void placeCursor(HANDLE, int, int);
    void printStars(int);
    void getData(HANDLE, Heading&);
    void displayReport(HANDLE, Heading);
};

int main()
{
    Heading display;
    HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
    display.getData(screen, display);
    display.displayReport(screen, display);
    cin.get();
    return 0;
}

Heading::Heading()
{
    companyName = "ABC Industries";
    reportName = "Report";
}

void Heading::placeCursor(HANDLE screen, int row, int col)
{
    COORD position;
    position.Y = row;
    position.X = col;
    SetConsoleCursorPosition(screen, position);
}

void Heading::printStars(int n)
{
    for(int star=1; star<=n; star++)
        cout << '*';
    cout <<endl;
}

void Heading:: getData(HANDLE screen, Heading &input)
{
    string str;
    placeCursor(screen, 2, 5);
    cout <<"Enter company name";
    placeCursor(screen, 2, 26);
    getline(cin, str);

    if(str!="")
        input.companyName = str;
    placeCursor(screen, 4, 5);
    cout<<"enter report name";
    placeCursor(screen, 4, 26);
    getline(cin, str);
    if(str!="")
        input.reportName = str;
}

void Heading::displayReport(HANDLE screen, Heading input)
{
    int l;
    placeCursor(screen, 8, 5);
    printStars(69);
    string str=input.companyName;
    l= str.length();
    l=39-l/2;
    placeCursor(screen, 9, 1);
    cout << str;
    str=input.reportName;
    l=str.length();
    l=39-l/2;
    placeCursor(screen, 10, 1);
    cout << str;
    placeCursor(screen, 11, 5);
    printStars(69);
}

Found several things wrong.

The algorithm for the text offset should be (overall length(69) - string length(str.length()) / 2 + screen offset(5).

When you were calling placeCursor, you were sending one for the column instead of the variable l.

Lastly cout << str; needed a << endl.

Here's the code with my changes:

void Heading::displayReport(HANDLE screen, Heading input)
{
    placeCursor(screen, 8, 5);
    printStars(69);
    string str=input.companyName;
    placeCursor(screen, 9, columnoffset(str.length(),69)+5);
    cout << str << endl;
    str=input.reportName;
    placeCursor(screen, 10, columnoffset(str.length(),69)+5);
    cout << str << endl;
    placeCursor(screen, 11, 5);
    printStars(69);
}
int Heading::columnoffset(int offset, int total)
{
    return (total - offset)/2;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.