I know that this is a simple one but never the less I forget how to do it.
How do I create white spaces on my screen.

Im writting in DEV-C++. I want my output to look something like this

Name title pay
Sam Uni student shit

I know how to output everything on the page but just don't know how to space them out. I know that I could do it by doing a cout statment followed by inverted comma and a shit load of spaces (or as required) inverted comma, semicolon. But that will look messy.

Im just chasing the simple code on how to create those spaces, my friend taught me and my text book sucks and it doesn't teach me.

Cheers
SAM

Dani AI

Generated

Quick follow-up to 's tip for : using stream manipulators is the right way to create tidy, maintainable columns in console output instead of stuffing literal spaces.

Useful points and pitfalls to keep in mind:

  • Pick a fixed column width (or compute each column's max length) and use a field width for every column so rows line up even when data lengths vary.
  • std::setw sets a minimum width for the very next insertion only.
  • std::left and std::right control justification and remain in effect until changed; std::setfill changes the padding character and persists.
  • If a value is wider than the width, it will not be truncated — it will push columns out. Truncation requires substr or an explicit formatter.
  • Tabs (\t) rely on tab stops and can produce misaligned output; HTML collapses runs of spaces (use <pre> or CSS there).
  • For modern codebases consider std::format (C++20) or the {fmt} library for clearer templates; printf also offers convenient width/justification specifiers.

Example pattern (header + row alignment):

#include <iostream>
#include <iomanip>
#include <string>

int main() {
    const int W1 = 12, W2 = 20, W3 = 8;
    std::cout << std::left  << std::setw(W1) << "Name"
              << std::setw(W2) << "Title"
              << std::right << std::setw(W3) << "Pay" << '\n';

    std::cout << std::left  << std::setw(W1) << "Sam"
              << std::setw(W2) << "Uni student"
              << std::right << std::setw(W3) << "0.00" << '\n';
}

If columns must be computed at runtime, scan the input to determine each column's max length (plus a small padding) and then print rows with those widths. For UTF-8 or wide characters, use libraries that measure display width rather than byte count.

Recommended Answers

All 2 Replies

If you are using cout then you can use setw()

#include <iostream>
#include <iomanip>
using namespace std;

int main(int argc, char* argv[])
{
    cout << setw(10) << "Hello";
    return 0;
}

Sweet, that was exactly what I was looking for. Thanks mate.

SAM

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.