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.

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 6 Years Ago by firstPerson: n/a

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";
    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 6 Years Ago by Nick Evan: n/a

This question has already been answered. Start a new discussion instead.