Im trying to align my screen ouput. At the moment Ive been using setw and \t. They don't seem to line up correctly. I dont know why the first one hasn't got the got the correct alignment or y its different from all the others. My program is pretty complecated(well atleast it is to me) so I CANNOT simply adjust the first type and wage by simply doing this

cout << "       ";

ive also tried the following

cout << setw(n);
cout << "\t\t";

I just need to know how to align these up properly. is there any other ways?

ID NAME TYPE WAGE
1 Sam Casual $1640
2 Serena Casual $1640
3 Bryce Manager $2000
4 Fred Manager $2000
5 Grey P/T $1000
6 Bluey P/T $1000

Also does anone know how to export your file to a CSV file? If so where can I learn? Are there some good tutorials on the net? How do I save the output in a text file with the contents marked up with HTML? If anyone knows where I can learn that stuff that would be great...

CHEERS

Dani AI

Generated

Mixing tabs, varying name lengths and the way the console expands tabs is almost always the culprit here. and are correct to think in terms of fixed columns; is right to warn about subtle CSV/format pitfalls. Practical fixes below: pick a column width for each field and produce output that always fills that width (no tabs), or use a formatter that pads fields for you.

A modern, compact approach (C++20) is to format each row with a single format string so columns are stable and easy to change:

auto row = std::format("{:<3} {:<12} {:>7}\n", id, name, wage);

If your toolchain lacks std::format, use stream manipulators with explicit alignment and fill (see setw docs). For Python the equivalent is:

row = "{:<3} {:<12} {:>7}".format(id, name, wage)

To export CSV reliably, escape fields that contain commas or quotes by wrapping in quotes and doubling internal quotes. Example escape helper:

std::string escape_csv(const std::string& s) {
    std::string out = "\"";
    for (char c : s) {
        if (c == '"') out += "\"\"";
        else out += c;
    }
    out += '"';
    return out;
}

Write rows with commas between escaped fields and save via an ofstream (see ofstream docs). For HTML output, write a simple table with <tr>/<td> rows and save as .html; open in a browser to get an aligned view.

Quick troubleshooting checklist:

  • Stop using \t for alignment; tabs jump to tab stops (usually every 8 columns).
  • Confirm console/editor uses a monospaced font.
  • Watch for hidden characters or multibyte/Unicode characters that occupy more than one column.
  • Test with small examples (few rows) until columns are stable, then apply the same formatting to your larger codebase.

For CSV format rules and corner cases see the CSV overview on Wikipedia: https://en.wikipedia.org/wiki/Comma-separated_values

Recommended Answers

All 3 Replies

you can use the width function

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

int main()
{
    cout.width(15);cout<<"Welcome\n";
    cout.width(15);cout<<"to DaniWeb\n";
cin.get();
return 0;
}

It will only help when you need right aligning.

The best way to deal with console text alignment issues is to get out a piece of graph paper and a stylus and draw what you want it to look like. Then you can count things out so that they always match.

As for the CSV stuff, give me a day or three to finish my CSV library (since the need seems to pop up regularly). CSV files are deceptively simple, so that caveats always catch people. The C++ version is pretty much done (it needs more testing still), but the C-version is a headache to deal with variable-length strings gracefully...

(You'll like the C++ version. It is all standard containers and can even translate data types for you using the standard stream operators... Its only about 300 lines of code total not counting boilerplate.)

Hey I have never tried what is posted below. But i think it might work out fine if you wish to align.

After the ID 01 And then you can give out a fixed length of setw(4);
After that when it comes to the NAME part maybe we can give it a space of (12) Characters in Total. And Then ..

string name;//This is where your name is stored
int id;
int x = (12-name.length());//Some variable which gives out the total value. So

cout<< id << setw(4); <<name <<setw(x);
//Might give you some result which is kinda starting at a the same point.

Or You can also try using spaces.

From the above.

void space (int a)
{
for (int b=0;b<=a;b++)
{
cout<<" ";
}

//Then in main something like this. 

int main()
{
cout<<ID;
space(4);
cout<<name;

Hope this work.

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.