Dynamically Allocating an array of structures

Pages: 123
It keeps adding it continuously, how would i go about resetting it?
What should the value of total be before you start reading scores for a student?

What if you set the value of total to be correct at that point?
I GOT IT TO WORK! Here is what I did.

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
void getInfo(Student students[], int size)
{

    double index = 0;
    double index2 = 0;
    double total = 0;
    double numTests = 4;

    for(int studentNum = 0; studentNum < size; studentNum++)
    {
        cout << "Student #" << index + 1 << " Name: ";
        cin >> students[studentNum].name;
        cout << "ID Number: ";
        cin >> students[studentNum].idNum;


        total = 0;   //Added your suggestion of a reset before reading the scores
        for(int i = 0; i < NUM_TESTS; i++)
        {
            cout << "      Test #" << index2 + 1 << ": ";
            cin >> students[studentNum].testScores[i];

           total += students[studentNum].testScores[i];

            students[studentNum].average = total / numTests;

            index2++;
        }

        index2 = 0;

        index++;

        cout << endl;
    }

}
Good.

Something to think about:
1
2
3
4
5
6
for ( int i = 0; i < NUM_TESTS; i++ )
{
    // change total, etc

     students[studentNum].average = total / numTests;
}

You write to students[studentNum].average on every iteration. NUM_TESTS times.

Q. What is the value of students[studentNum].average after the loop?
A. Whatever was written on the last iteration.

Why write anything before the last iteration? Could you calculate the average after the loop, rather than in the loop?


Your loop adds NUM_TESTS values to total, but you use numTests when calculating the average. Why?
Congratulations!

Now here are some suggestions to improve your code. I completely understand if you want to ignore these. If you choose to try them, SAVE YOUR WORK first. You don't want to try to "improve" the code and end up with a non-working program when it's time to turn it in.

Okay, here are some thoughts.
- You don't need variables index and index2 because studentNum and i do the trick. Change 11 to:
cout << "Student #" << studentNum + 1 << " Name: "; and change line 20 to:
cout << " Test #" << i + 1 << ": ";

Also, there's no reason to update average each time through the inner loop. Only the last value matters. So move line 25 to line 29 so it's outside the inner loop.
I had one more question I am trying to store the average into the grade member of the student struc to match it with a grade, how would I go about it?

Here's is what I thought would of worked but it does not :

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
void getInfo(Student students[], int size)
{

   //double index = 0;
   //double index2 = 0;
    double total = 0;

    for(int studentNum = 0; studentNum < size; studentNum++)
    {
        cout << "Enter Student #" << studentNum + 1 << " First name: ";
        cin >> students[studentNum].fName;
        cout << "Enter Student #" << studentNum + 1 << " Last name: ";
        cin >> students[studentNum].lName;
        cout << "ID Number: ";
        cin >> students[studentNum].idNum;


        total = 0;
        for(int i = 0; i < NUM_TESTS; i++)
        {
            cout << "      Test #" << i + 1 << ": ";
            cin >> students[studentNum].testScores[i];

           total += students[studentNum].testScores[i];

            //index2++;
        }


            students[studentNum].average = total / NUM_TESTS;
            students[studentNum].grade = students[studentNum].average;

            if(students[studentNum].grade >= 91 || students[studentNum].grade <= 100)
            {
                students[studentNum].grade = 'A';
            }
            if(students[studentNum].grade >= 91 || students[studentNum].grade <= 90)
            {
                students[studentNum].grade = 'B';
            }
            if(students[studentNum].grade >= 71 || students[studentNum].grade <= 80)
            {
                students[studentNum].grade = 'C';
            }
            if(students[studentNum].grade >= 61 || students[studentNum].grade <= 70)
            {
                students[studentNum].grade = 'D';
            }

        //index2 = 0;

        //index++;

        cout << endl;
    }

}
Last edited on
If you have:
1
2
3
4
5
struct Student
{
    double average;
    char grade;
};

Then you can't write a double value to member 'grade'.

You can test students[studentNum].average >= 91


Lets test your logic. Your average is 90.5. What grade is that?
if the average is 90.5% should be B

I realized I had a typo as well in my if statement,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
            if(students[studentNum].grade >= 91 || students[studentNum].grade <= 100)
            {
                students[studentNum].grade = 'A';
            }
            if(students[studentNum].grade >= 81 || students[studentNum].grade <= 90)
            {
                students[studentNum].grade = 'B';
            }
            if(students[studentNum].grade >= 71 || students[studentNum].grade <= 80)
            {
                students[studentNum].grade = 'C';
            }
            if(students[studentNum].grade >= 61 || students[studentNum].grade <= 70)
            {
                students[studentNum].grade = 'D';
            }


I just did not include any decimal numbers since the range of the grades on the assignment is whole numbers
Last edited on
if(students[studentNum].grade >= 91 || students[studentNum].grade <= 100)

Every number is greater than 91 or less than 100. For example, 0 is less than 100. So is 95. 200 is greater than 91, so is 10 million.
In addition to what dhayden said:

You're trying to use the grade member of Student for 2 entirely different things - to store the average result, and then to store a single-character grade. That makes no sense at all.

I don't understand why you're copying average into grade at all. What is the point of that? Why do you not simply test the value of average?
You are right I do not know why I was trying to store double integer into a char, how i rewrote it was that if the average is in a range of number then char should equal a letter like so:

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
void getInfo(Student students[], int size)
{

   //double index = 0;
   //double index2 = 0;
    double total = 0;

    for(int studentNum = 0; studentNum < size; studentNum++)
    {
        cout << "Enter Student #" << studentNum + 1 << " First name: ";
        cin >> students[studentNum].fName;
        cout << "Enter Student #" << studentNum + 1 << " Last name: ";
        cin >> students[studentNum].lName;
        cout << "ID Number: ";
        cin >> students[studentNum].idNum;


        total = 0;
        for(int i = 0; i < NUM_TESTS; i++)
        {
            cout << "      Test #" << i + 1 << ": ";
            cin >> students[studentNum].testScores[i];

           total += students[studentNum].testScores[i];

            //index2++;
        }


            students[studentNum].average = total / NUM_TESTS;

            if(students[studentNum].average >= 91 || students[studentNum].average <= 100)
            {
                students[studentNum].grade = 'A';
            }
            if(students[studentNum].average >= 81 || students[studentNum].average <= 90)
            {
                students[studentNum].grade = 'B';
            }
            if(students[studentNum].average >= 71 || students[studentNum].average <= 80)
            {
                students[studentNum].grade = 'C';
            }
            if(students[studentNum].average >= 61 || students[studentNum].average <= 70)
            {
                students[studentNum].grade = 'D';
            }

        //index2 = 0;

        //index++;

        cout << endl;
    }

}
That makes more sense with regard to which variables you use, but you've still ignored what dhayden said.
Would it be instead

1
2
if(students[studentNum].grade >= 91 && students[studentNum].grade <= 100)


since it has to meet both conditions of it being greater than or equal to 91 and less than or equal to 100
Yep, that's right. Although you could easily have tried it for yourself.

You could even use your debugger to step through your code, so that you can see exactly which lines are executing, and can see which if statements are evaluating to true or false.
I got it to work the way it should :)
Topic archived. No new replies allowed.
Pages: 123