Iomanip, confusion

So when you enter in the data it display something like this
1
2
Id      Name     test1      test2     test3    test4    test5   GPA
1  smith,  steve   100        100      100       50       50     80

but if i enter a larger name like

first name: onymou
last name: starcraft two

then everything shifts. is there a way where the number will not shift even if the name gets longer?


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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <string>
#include <iomanip>
#include <windows.h>

using namespace std;

const int SCORES = 5;
struct Student
{
    string firstName, lastName;

    int studentID,
        testScores[SCORES],
        studentGPA;
};

void getStudentRecords(Student studentInfo[], int SIZE, int& count );
void printInformation(const Student studentInfo[ ], int SIZE, int& count  );

int main( )
{
    const int SIZE = 10;
    int count = 0;

    Student studentInfo[ SIZE ];

    cout << "\t\t\tRecording Students information.\n\n";

    getStudentRecords(studentInfo, SIZE, count );

    printInformation( studentInfo, SIZE, count );


    system( "pause" );
    return( 0 );
}

void getStudentRecords(Student studentInfo[], int SIZE, int& i )
{
    char answer;
    //int count = 1;

        do 
        {
            cout << "Enter student ID: ";
            cin >> studentInfo[i].studentID;
            cin.ignore( );
    
            cout << "Enter studnet first name: ";
            getline(cin, studentInfo[i].firstName);
    
            cout << "Enter student last name: ";
            getline( cin, studentInfo[i].lastName);
            
            studentInfo[ i ].studentGPA = 0;
    
            for ( int x = 0; x < 5; x++ )
            {
    
                cout << "Enter test scores " << x + 1 << ": ";
                cin >> studentInfo[i].testScores[x];
    
                studentInfo[i].studentGPA = studentInfo[i].studentGPA + studentInfo[i].testScores[x];
    
            }
    

            cout << "Enter another student? y/n: ";
            cin >> answer;
            
            i++;
    
        } while( answer == 'y' );
        
        system( "cls" );
        
       
}       


void printInformation(const Student studentInfo[], int SIZE, int& i )
{

    cout << left << setw(10) << "Id"
         << setw(15) << "Name"
         << setw(10) << "test 1"
         << setw(10) << "test 2"
         << setw(10) << "test 3"
         << setw(10) << "test 4"
         << setw(10) << "test 5"
         << setw(10) << "GPA" << endl;

    for ( int j = 0; j < i; j++ )
    {
        cout << left << setw(3)
             << studentInfo[j].studentID
             << left <<  " " 
             << setw( 9 ) << studentInfo[j].lastName
             << left << "," << studentInfo[j].firstName
             << right << " "
             << setw( 9 ) << studentInfo[j].testScores[0]
             << right << " " 
             << setw( 9 ) << studentInfo[j].testScores[1]
             << right << " "
             << setw( 9 ) << studentInfo[j].testScores[2]
             << right << " "
             << setw( 9 ) << studentInfo[j].testScores[3]
             << right << " "
             << setw( 9 ) << studentInfo[j].testScores[4]
             << right << " "
             << setw( 5 )<< " " << studentInfo[j].studentGPA / 5 << endl;
    }
}
The only way would be truncating the strings longer than the maximum number of spaces.
If you increase the spacing, you'll risk of having your text wrapping to the new line in the terminal window
Topic archived. No new replies allowed.