output formatting

how do i use the setw if I want following output where first and last names are of different lengths for students.
The name formatting has to be strictly left justified and in order as last name,
followed by comma, followed by space, followed by first name. I am able to output this but cannot get the Test scores and grades in a perfect column.

Student name Test Score Grade
Darji, Nilesh 25 F
Smith, John 90 A
Taylor, Michael 85 B
Subramanian, Ashok 75 C


1) Find maxumum length of your resulting name string and store it in variable
2) Output name with setw() using this variable:
std::cout << std::setw(maxlength) << (last_name + ", " + first_name) << grade /*...*/
Try this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// ConsoleApplication3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>

using namespace std;

int name = 20;
int test_score = 20;
int grade = 20;

int _tmain(int argc, _TCHAR* argv[])
{
	cout << setw(name) << left << "name";
	cout << setw(test_score) << left << "test score" ;
	cout << setw(grade) << left << "grade" << endl;

	cout << setw(name) << left << "Darji, Nilesh"; 
	cout << setw(test_score) << left << "25";
	cout << setw(grade) << left << "F" << endl;
	
	int x;
	cin >> x;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Actually none of the above will work. I think I did not asked the question correctly. I have a struct as follows:
struct studentType
{
    string studentFName;
    string studentLName;
    int testScore;
    char grade;
};

Now there is a text file that has student information as under in order of first name, followed by last name followed by testscore:
Nilesh Darji 25
John Smith 90
Michael Taylor 85
Ashok Subramanian 75

I have to read it and store it in the struct variables which I am able to do well. Problem is the requirement of the output. It has to be in following order:

Student name                  Test Score                     Grade
Darji, Nilesh                           25                              F
Smith, John                            90                             A
Taylor, Michael                       85                              B
Subramanian, Ashok               75                              C

Now because the first and last names are in separate string variables of different lengths, I am finding it hard to make the output look like above. Because how can I set testscore column start point as the point at which the names end are varying. 

Did I ask correctly this time. Please let me know. Thanks for helping me.
Last edited on
Actually none of the above will work
Reread my post again.
I suggested to calculate maximum length of resulting string (lenght of firstname + lastname + 2) or just throwq some value gigh enough.
Then to solve problem of differrent length, you need to combine them in one and output it with setw:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <iomanip>
#include <string>

struct studentType
{
    std::string studentFName;
    std::string studentLName;
    int testScore;
    char grade;
};

int main()
{
	std::cout << std::left;
    studentType students[] = {
    	{"Nilesh", "Darji", 25, 'F'},   {"John", "Smith", 90, 'A'},
    	{"Michael", "Taylor", 85, 'B'}, {"Ashok", "Subramanian", 75, 'C'}};
	const std::size_t maxw = 20; //I am lazy
	std::cout << std::setw(maxw) << "Student name" << std::setw(11) << "Test Score" <<
	             std::setw(6) << "Grade\n";
	for(const auto& r: students)
		std::cout << std::setw(maxw) << (r.studentLName + ", " + r.studentFName) <<
		             std::setw(11) << r.testScore << std::setw(6) << r.grade << '\n';
}
Student name        Test Score Grade
Darji, Nilesh       25         F
Smith, John         90         A
Taylor, Michael     85         B
Subramanian, Ashok  75         C
Oh Yes. I have to say this. You are a genius. Did not know that setw can also be applied on adding strings in (). Very good. Really appreciate. Thanks a lot genius.
setw() applies to next output. Logical choice if you need to set width for several string at once is to output them at once.
Topic archived. No new replies allowed.