output problem: setw related

I am having a formatting issue that seems like it should be easy to figure out but I am having trouble. I need the format as follows.

Smith, John 3.50
Jones, Tom 3.50
Mincher, fred 3.50

so I figured this would be the code.

1
2
         std::cout << lastName << ", " << firstName << setw(15) << gpa[j]
                   << endl;


The output changes with the amount of characters of the names.

Smith, John 3.50
Jones, Tom 3.50
Mincher, Fred 3.50

I have messed with it quite a bit with << left << right but it's just not working. I could right justify the names which would solve the problem. However, I would like to figure out to get it the way I want. Thank you for your time.
and the format got messed up
1
2
3
4
5
6
7
8
9
10
11
What I need:
Smith, John               3.50
Jones, Tom                3.50
Mincher, fred             3.50 

What I am getting
Smith, John             3.50
Jones, Tom            3.50
Mincher, fred              3.50 

Last edited on
closed account (48T7M4Gy)
<< setw(15) << left << (lastname + ", " + firstname) << setw(15) << right << gpa << '\n';
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

void myOutput( string first, string last, double gpa )
{
   string fullname = last + ", " + first;
   cout << left << setw(20) << fullname;
   cout << fixed << setw(4) << setprecision(2) << gpa << endl;
}

int main()
{
   string first, last;
   double gpa;

   first = "John";   last = "Smith";     gpa = 2.5;   myOutput( first, last, gpa );
   first = "Tom";    last = "Jones";     gpa = 3.5;   myOutput( first, last, gpa );
   first = "Fred";   last = "Mincher";   gpa = 3.0;   myOutput( first, last, gpa );
}


Smith, John         2.50
Jones, Tom          3.50
Mincher, Fred       3.00

Last edited on
Ah, I think I understand. What happens if the name is over 15/20 characters? I am going to go write this up thank you. This is that same program I was working on yesterday kemort.

The program is here:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>

using namespace std;

int main()
{
    static const int numberOfRecords = 10;
    string firstName[numberOfRecords];
    string lastName[numberOfRecords];
    float gpa[numberOfRecords];

    cout << "Welcome to Student Record's Information" << endl;

    cout << "Please Enter Student details.\n";
    for(int i = 0; i < numberOfRecords; i++){

        cout << "Enter the first and last name of student #" << i+1 << endl;
        cin >> firstName[i] >> lastName[i];

        cout << "\nEnter GPA of student #" << i+1 << endl;
        cin >> gpa[i];

        if(gpa[i] < 0 || gpa[i] > 4.00){
            cout << "Error: Re-Enter GPA ";
            cin >> gpa[i];
        }//Ends if statement
    }//End of for loop

    for(size_t i = 0; i < numberOfRecords; ++i){
        firstName[i][0] = toupper(firstName[i][0]);
        lastName[i][0] = toupper(lastName[i][0]);
        }//Ends toupper for loop

    cout << "Name (Last, First)" << setw(15) << "GPA\n"
         << "-------------------------\n";
    cout << fixed << showpoint << setprecision(2);
    for(int j = 0; j < numberOfRecords; j++){
        cout << lastName[j] << " , " << firstName[j] << setw(15) << gpa[j]
             << endl;
    }//End of for loop

}//Ends main.
well I tried this output loop which went horribly wrong

1
2
3
4
    for(int j = 0; j < numberOfRecords; j++){
        cout << left << setw(15) << lastName[j] << " , "
             << firstName[j] << right << setw(15) << gpa[j]
             << endl;


the first name ended up 15 spaces to the right of the last name?
What happens if the name is over 15/20 characters?


You have two choices - you increase the available space (by setting a larger width), or you shorten any excess-length strings (use the substr() method).

If you wished you could find the maximum length of all strings and send it as a parameter to the output routine to include in setw(). This seems over-the-top. 20 characters ought to be plenty.
lastchance-

I am going to set a loop to assign a full name and try your code. I know I am missing something small, I am so close.


Ok I tried it out with this: (partial code). I had no output

1
2
3
4
5
6
7
8
9
        for(int i =0; i < numberOfRecords; i++){
        	string fullName[i] = lastName[i] + ", " + firstName[i];
        }
    cout << "Name (Last, First)" << setw(15) << "GPA\n"
         << "-------------------------\n";
    cout << fixed << showpoint << setprecision(2);
    for(int j = 0; j < numberOfRecords; j++){
    	   cout << left << setw(20) << fullName[j];
    	   cout << fixed << setw(4) << setprecision(2) << gpa[j] << endl;
Last edited on
setw sets the minimum width of the next thing that is outputted. If you want the first column to contain both the last and first name you need to combine them so that you can output them with one output operation (<<).

1
2
3
cout << left  << setw(15) << (lastName[j] + " , " + firstName[j]) // first column
     << right << setw(15) << gpa[j] // second column
     << endl;
I am an idiot. I missed a minute detail of adding lastname + firstname in my first output. The code is fixed thank you fellas so much!

Final Code =
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50


#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>

using namespace std;

int main()
{
    static const int numberOfRecords = 10;
    string firstName[numberOfRecords];
    string lastName[numberOfRecords];
    float gpa[numberOfRecords];

    cout << "Welcome to Student Record's Information" << endl;

    cout << "Please Enter Student details.\n";
    for(int i = 0; i < numberOfRecords; i++){

        cout << "Enter the first and last name of student #" << i+1 << endl;
        cin >> firstName[i] >> lastName[i];

        cout << "\nEnter GPA of student #" << i+1 << endl;
        cin >> gpa[i];

        if(gpa[i] < 0 || gpa[i] > 4.00){
            cout << "Error: Re-Enter GPA ";
            cin >> gpa[i];
        }//Ends if statement
    }//End of for loop

    for(size_t i = 0; i < numberOfRecords; ++i){
        firstName[i][0] = toupper(firstName[i][0]);
        lastName[i][0] = toupper(lastName[i][0]);
        }//Ends toupper for loop

    cout << "Name (Last, First)" << setw(15) << "GPA\n"
         << "-------------------------\n";
    cout << fixed << showpoint << setprecision(2);

    for(int j = 0; j < numberOfRecords; j++){
        cout << setw(15) << left << (lastName[j] + ", " + firstName[j]) << right << setw(15) << gpa[j]
             << endl;
    }//End of for loop

}//Ends main.



Topic archived. No new replies allowed.