1

I'm struggling getting my columns to align on an assignment I have for school. The directions are to ensure that each column heading aligns correctly with its respective column. I've tried everything I've found online so far, including the cout.width() and the setw(), neither of which has worked. I'm hoping it's not due to me implementing these methods incorrectly, though I can't think of any other reason why they wouldn't work. The assignment specifically has 2 student names to use as an example. Jones, Bob and Washington, George. Due to the major difference in the number of characters between one student and the next, the columns just won't align. I know that setting a column width should fix that, but in my case it isn't. Then code I'm supplying is using the setw() method, but I can supply how I tried using the cout.width() if needed. Any help is GREATLY appreciated.

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

using std::cout;
using std::cin;
using namespace std;

const int ARRAYSIZE = 2;

struct studentData{
    string lastName;
    string firstName;
    float studentGpa;
}studentArray[ARRAYSIZE];

void displayData(int a);

int main()
{
    int counter = 0;


    for (int a = 0; a < ARRAYSIZE; a++)
    {
        cout << "Enter last name: " << endl;
        cin >> studentArray[a].lastName;

        cout << "Enter first name: " << endl;
        cin >> studentArray[a].firstName;

        cout << "Enter GPA: "<< endl;
        cin >> studentArray[a].studentGpa;

        counter++;
    }
    cout << "\n\n";
    displayData(counter);

return 0;
}

void displayData(int a)
{
    int newSize = a;

    cout << left << setw(20) <<"\nName(Last, First)";
    cout << right << setw(20) << "GPA\n";

    for (int z = 0; z < newSize; z++)
    {
        cout << studentArray[z].lastName << ", " << studentArray[z].firstName;
        cout << right << setw(20) << fixed << setprecision(2) <<studentArray[z].studentGpa << endl;
    }
}

And my console input/output:

Enter last name: 
Jones
Enter first name: 
Bob
Enter GPA: 
3.0
Enter last name: 
Washington
Enter first name: 
George
Enter GPA: 
4.0



Name(Last, First)                  GPA
Jones, Bob                3.00
Washington, George                4.00
BoBTFish
  • 19,167
  • 3
  • 49
  • 76
JoeX
  • 37
  • 10

1 Answers1

2

You are along the right lines, but std::setw only applies to the next output operation. If you turn printing the name into one operation, instead of 3 (last, comma, first) then you can pad the width of that more easily:

void displayData(std::size_t a)
{
    std::cout << std::left
              << std::setw(20)
              << "Name(Last, First)"
              << "GPA\n";

    for (std::size_t z = 0; z < a; ++z)
    {
        std::cout << std::left
                  << std::setw(20)
                  << (studentArray[z].lastName + ", " + studentArray[z].firstName)
                  << std::fixed << std::setprecision(2)
                  << studentArray[z].studentGpa
                  << '\n';
    }
}

Output:

Name(Last, First)   GPA
Jones, Bob          3.00
Washington, George  4.00

Here, both for printing the column headings, and for the student names, I use std::left to say that the content should be at the left of the padded total, and std::setw to pad this output operation to 20 characters total (by default it will pad with spaces).

In both cases, this is the 1st column in the output, so I don't need to do anything with the 2nd column. If you had a 3rd column, you would need to pad the 2nd as well.

I also replaced your ints with size_ts for array indexing. It's a little point, but you shouldn't really use a signed type for indexing into a container where accessing a negative index would be Undefined Behaviour.


Also, please reconsider your use of what are often considered bad practices: using namespace std; and endl (those are links to explanations).

Community
  • 1
  • 1
BoBTFish
  • 19,167
  • 3
  • 49
  • 76
  • 1
    You're amazing. Thank you so much. Thanks for the links on the bottom too, very good to know. – JoeX Apr 01 '17 at 18:56