0

I want to center this.

cout<<a<<" "<<"+ x = "<<b<<endl;

a and b are both int. How would I go about this. Most of the information I found on the web is about centering strings, I tried the code but did not have any success.

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Nick Evan
1

There is no way in C++, but you can do something like this :

void printN(char c, int n){
	while(n--) cout << c;
}
int main(){
 int a = 1, b = 2;
 const int ADJUSTMENT = 30;
 printN(' ',ADJUSTMENT);
 cout<<a<<" "<<"+ x = "<<b<<endl;
}

Edited by firstPerson: n/a

0

Or on windows, you might even try:

#include <windows.h>
#include <iostream>
#include <iomanip>
#include <string>

int main()
{
    const std::string the_string = "Some text";
    CONSOLE_SCREEN_BUFFER_INFO info;
    GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &info ); // Get information about the output window

    unsigned int offset = (info.dwSize.X / 2) - (the_string.length() / 2); // take half of the screen width and subtract half of the stringlength
    std::cout << std::setw(offset) << the_string; // use setw to set the calculated offset
}

Edited by Nick Evan: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.