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;
}

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
}
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.