I'm thinking loop trouble any ideas

This programming I'm making should take in 2 student's names, student ids, and 5 grades. but when it gets to taking in grades it only takes in one, and at the end of the program it only prints the first student i enter and give the wrong average. I know the table isn't perfect and the average is really just the sum but i'm really not to worry about that i just need some help with the loop and why it isn't working

thanks for all your help.. I'm not asking anyone to do this for me but if you can turn me in the right direction it would be appreciated...

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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

struct Student
{
    string fullName;

    int studentID,
        testScores[ ],
        studentGPA;
};

int main( )
{
    const int SIZE = 2;

    Student studentInfo[ SIZE ];

    studentInfo[ SIZE ].studentGPA = 0;

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

    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);

        for ( int x = 0; x < 5; x++ )
        {

            cout << "Enter test scores: ";
            cin >> studentInfo[SIZE].testScores[x];

            studentInfo[SIZE].studentGPA = studentInfo[SIZE].studentGPA + studentInfo[SIZE].testScores[x];

        }
    }

    cout << left << setw(22) << "Id"
         << setw(20) << "Name"
         << setw(35) << "Average" << endl;

    for ( int j = 0; j < 1; j++ )
    {
        cout << left << setw(22)
             << studentInfo[j].studentID
             << right << " " << setw( 5 ) << studentInfo[j].fullName
             << setw(7)<< " " << studentInfo[SIZE].studentGPA << endl;
    }


    return( 0 );
}

1
2
3
4
5
6
7
8
struct Student
{
    string fullName;

    int studentID,
        testScores[ ],
        studentGPA;
};


That isn't valid C++ code. Please post a program that will actually compile.
studentInfo[ SIZE ].studentGPA = 0;

Array indices will range from 0..SIZE-1. That code results in undefined behavior. In fact you do this in a variety of places where you need to be using the variable i instead of the SIZE constant.
Last edited on
kempofighter thanks for responding but i really not sure what your saying. I see where I messed up in the loop so i chanced all the SIZE to the loop var. but now instead of the programming letting me enter one test score it lets me enter 3 but i need it to enter 5. I sure the number are correct.....

and this program complies in my IDE code::blocks ? I don't see why that isn't valid C++ code.. here is what i chanced..

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

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

using namespace std;

struct Student
{
    string fullName;

    int studentID,
        testScores[ ],
        studentGPA;
};

int main( )
{
    const int SIZE = 2;

    Student studentInfo[ SIZE ];

    studentInfo[ SIZE ].studentGPA = 0;

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

    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);

        for ( int x = 0; x < 5; x++ )
        {

            cout << "Enter test scores: ";
            cin >> studentInfo[x].testScores[x];

            studentInfo[x].studentGPA = studentInfo[x].studentGPA + studentInfo[x].testScores[x];

        }
    }

    cout << left << setw(22) << "Id"
         << setw(20) << "Name"
         << setw(35) << "Average" << endl;

    for ( int j = 0; j < 1; j++ )
    {
        cout << left << setw(22)
             << studentInfo[j].studentID
             << right << " " << setw( 5 ) << studentInfo[j].fullName
             << setw(7)<< " " << studentInfo[SIZE].studentGPA << endl;
    }


    return( 0 );
}

As has been stated before, line 23 is going out of bounds on the area. When you declare an array of SIZE on line 21, you have elements studentInfo[0], studentInfo[1], ..., studentInfo[SIZE-1] only. studentInfo[SIZE] is outside of the array's bounds.

In your line 36 for loop, I think you meant studentInfo[i] instead of studentInfo[x], and using x would repeatedly overwrite the first 5 student's test scores instead of the person you are current working on.

Also, on line 13, you can't declare an array without a size like that as far as I know. Even if you could, you'll just be getting a pointer anyway. You probably want to make that an array of size 5 if your line 36 for loop is any indication.
Topic archived. No new replies allowed.