I think its a looping problem

this program takes in 10 students or less but when you enter less then 10 it seem that the loop keeps going and gets random junk from memory can anyone help me out here.. point me in the right direction. thank you.... and thank you if you help me with my last problem with this program...


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

#include <iostream>
#include <string>
#include <iomanip>
#include <windows.h>

using namespace std;

const int SCORES = 5;
struct Student
{
    string fullName;

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

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

int main( )
{
    const int SIZE = 10;

    Student studentInfo[ SIZE ];

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

    getStudentRecords(studentInfo, SIZE );

    printInformation( studentInfo, SIZE );


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

void getStudentRecords(Student studentInfo[], int SIZE )
{
    char answer;
    
    for ( int i = 0; i < SIZE; i++ )
    {
        cout << "Enter student ID: ";
        cin >> studentInfo[i].studentID;
        cin.ignore( );

        cout << "Enter studnet full name: ";
        getline(cin, studentInfo[i].fullName);
        
        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];
            
        }
        
        Start:
        cout << "Enter another student? y/n: ";
        cin >> answer;
        
        if ( answer == 'y' )
        {
             continue;
        }
        else if ( answer == 'n' ) 
        {
             system( "cls" );
             break;
         }
         else 
         {
              goto Start;
         }
    }
}


void printInformation(const Student studentInfo[], int SIZE )
{
     
    cout << left << setw(10) << "Id"
         << setw(15) << "Name"
         << setw(8) << "test 1"
         << setw(8) << "test 2"
         << setw(8) << "test 3"
         << setw(8) << "test 4"
         << setw(8) << "test 5"
         << setw(10) << "GPA" << endl;

    for ( int j = 0; j < SIZE; j++ )
    {
        cout << left << setw(5)
             << studentInfo[j].studentID
             << right << setw( 20 ) << studentInfo[j].fullName
             << left << " " 
             << setw( 8 ) << studentInfo[j].testScores[0]
             << setw( 8 ) << studentInfo[j].testScores[1]
             << setw( 5 ) << studentInfo[j].testScores[2]
             << right 
             << setw( 5 ) << studentInfo[j].testScores[3]
             << setw( 7 ) << studentInfo[j].testScores[4]
             << setw( 7 )<< " " << studentInfo[j].studentGPA / 5 << endl;
    }
}

You array is a constant size 10, so it will always have 10 students, even if the user only fills out less. The minimal change solution would probably be to add another variable that indicates the number of students that were actually entered and only loop until that instead of the full size of the array.
Topic archived. No new replies allowed.