Search code examples
c++debuggingsetw

C++ Beginner: How do I prevent the text in my table from pushing any text to the right of it?


I am working on a very basic program for my Fundamentals I class and I have everything 98% working as intended.

This program takes the names of three grades, averages them, and outputs them into a table, but since assignmentName[] is on the same line of code as grade[], it pushes grade[] to the right determining on how many characters the user inputted. Screenshot of the problem

Here is the code I currently have written for the table:

cout << "___________________________\n";
cout << name << "'s Grade Chart\n";
cout << "---------------------------\n";
cout << setprecision(1) << fixed;
cout << "Grade for " << assignmentName[0] << setw(8) << grade[0] << endl;
cout << "Grade for " << assignmentName[1] << setw(8) << grade[1] << endl;
cout << "Grade for " << assignmentName[2] << setw(8) << grade[2] << endl;
cout << "\nYour average grade between those three assignments is: " << setw(1) << avg << endl;`

Solution

  • I commented, "Place another setw(N) where N is a bit bigger than the largest assignmentName before each << assignmentName."

    But on second thought it's bit more fun than that, so I figure a real answer is in order.

    First, some reading materials:

    Documentation on std::left and std::right

    Documentation on std::max

    And now on with the show!

    First we need to know how big the largest assignment name is.

    size_t max = 0;
    for (const string & assn: assignmentName)
    {
        max = std::max(max, assn.length());
        // You may need 
        //max = std::max(max, strlen(assn));
        // if you've been forced to resort to barbarism and c-style strings
    }
    max++; // one extra character just in case we get a really long grade.
    

    Sometimes this can get a lot neater. For example std::max_element can eliminate the need for the loop we used to get the maximum assignment name length. In this case we're looking for the size of the string, not the lexical order of the string, so I think the loop and std::max is a bit easier on the brain.

    And now to format, we print the names left-justified and the grades right justified, with the names padded max characters and the grades 8 characters.

    cout << "Grade for " << std::left << setw(max) << assignmentName[0] 
         << std::right << setw(8) << grade[0] << '\n' 
         << "Grade for " << std::left << setw(max) << assignmentName[1] 
         << std::right << setw(8) << grade[1] << '\n' 
         << "Grade for " << std::left << setw(max) << assignmentName[2] 
         << std::right << setw(8) << grade[2] << '\n';
    

    Note it's now one big cout. This was done mostly for demonstration purposes and because I think it looks better. It doesn't really save you much, if anything, in processing time. What does save time is the lack of endls. endl is actually a very expensive operation because not only does it end a line, but it also flushes. It forces whatever has been buffered in the stream out to the underlying media, the console in this case. Computers are at their best when they can avoid actually going out of the computer until they really have to. Drawing to the screen is way more expensive than writing to RAM or a cache, so don't do it until you have to.