-1

When I print the results of my program to the screen, the data is all over the place but I want to align the results as best as possible. I am using setw(10) in order to do this but it's not working properly can someone help me?

Thanks

This is what the output looks like:

enter image description here

This is my print function:

void Print(const call_record *call_DB, int & count)
{

    cout << fixed << showpoint << setprecision(2);
    for(int i= 0; i <= count; i++)
    {
        cout << call_DB[i].firstname <<" " << call_DB[i].lastname <<"  "  << call_DB[i].cell_number <<setw(15) << call_DB[i].relays <<setw(10) << call_DB[i].call_length;
        cout <<setw(10) << call_DB[i].net_cost <<setw(10) << call_DB[i].call_rate << setw(10) << call_DB[i].call_tax <<setw(10) << call_DB[i].total_cost;

        cout << endl;


    }
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241

2 Answers2

1

You must, of course, also set the width of the output of the strings (or of the combined name):

cout << std::setw(20) << (call_DB[i].firstname + call_DB[i].lastname);

(assuming that these are std::strings such that + concatenates them).

Walter
  • 44,150
  • 20
  • 113
  • 196
1

One of the best practices I have known on C++ output is using template functions.

Sample:

Your template function:

template<typename T> void coutElement(T t, const int& width)
{
    cout << std::left << setw(width) << setfill(separator) << t;
}

Using it:

const int name_width = 10; //non sense value
const int num_width  = 10; //non sense value
coutElement("Dainer", name_width);
coutElement("Doe", name_width);
coutElement(250, num_width);

This has always worked flawlessly, the only challenging thing about it is the decision you'll take for the values of name_width and num_width

Oussama Ben Ghorbel
  • 2,132
  • 4
  • 17
  • 34