Beginners struggles

So I was instructed to wright the following.

Student grade records are stored in “Student Data.txt” file.

Your tasks are:

define a structure for student grade records for data
stored in file with additional fields for average exam
score and grade.
2. declare an array of records on the stack or
( optional: on the heap ).
3. populate from the file: “Student Data.txt”.
4. define a function to display student records (formatted).
5. define a function to compute average and
populate average field.
6. define a function to compute the grade and
populate grade field.
7. define a function to compute class average.
8. define a function to compute class standard deviation.
9. display class average and standard deviation.
10. demonstrate ALL functions.

I have done this but I am coming up with some problems that I can not seem to fix. Thanks so much for the help!

1. I need some how to use the structure which I have made but don't know how to use.

2. I am having problems using my different variables arose all my functions. (I can not use globule functions)

below is the code and below that is whats in the file.

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//  main.cpp
//  Program_10
//  Created by William Blake Harp on 8/1/14.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void displaystudentrecords();
void computeaverage();
void computethegrade();
void classaverage();
void classstandarddeviation();

int main()
{
    double average = 0;
    double& averageRef = average;
    double class_average = 0;
    double& class_averageRef = class_average;
    int sum_of_test_scores = 0;
    int& sum_of_test_scoresRef = sum_of_test_scores;
    double standard_deviation = 0;
    
    //2D Array.
    const int students = 5;
    const int test_scores = 4;
    int scores[students][test_scores];
    
    //Arrays on the heap.
    string* arrayNames = nullptr;
    arrayNames = new string[students];
    string* arrayNamesRef = arrayNamesRef;
    
    int* arrayID = nullptr;
    arrayID = new int[students];
    
    int* arrayAverage = nullptr;
    arrayAverage = new int[students];
    
    int* arrayLetterGrade = nullptr;
    arrayLetterGrade = new int[students];
    
    ifstream inputFile;
    // Open the file.
    inputFile.open("Student Data.txt");
    
    // do an initial test.
    cout<<boolalpha;
    cout<< "Is the file good? --> "<<inputFile.good()<<endl<<endl;
    
    if (inputFile.fail())
        cout << "Can't open the file!" << endl;
    else
    {
        for( int i(0); i != 5; i++ )
        {
            // read name.
            getline(inputFile, arrayNames[i]);
            
            // read student id.
            inputFile >> arrayID[i];
            
            // read four(4) scores.
            for( int j(0); j != 4; j++ )
            {
                inputFile >> scores[i][j];
            }
            
            string str;
            
            // consume '\n' char (Use getline for mac/Xcode!!!).
            getline (inputFile, str);
            
            cout << "I have read "<<i+1 <<" record/s\n\n";
        }// end read loop.
        
        struct studentinfo //(grade records for data stored in file with additional fields for average exam score and grade.)
        {
            int student_grade_records;
            double average_exam_score;
            int grade;
        };
        
        displaystudentrecords();
        classaverage();
        classstandarddeviation();
    }
    return 0;
}// End Main.

void displaystudentrecords()
{
    for( int i(0); i != 5; i++ )
    {
        cout << "Name: "<< arrayNames[i];
        
        cout << "ID: " << arrayID[i] <<endl;
        
        for( int j(0); j != 4; j++ )
        {
            cout << "Test scores: " << scores[i][j] << endl;
            
            sum_of_test_scores += scores[i][j];
        }
        cout << endl;
        computeaverage();
    }
}// End displaystudentrecords.

void computeaverage()
{
    average = sum_of_test_scores / test_scores;
    cout << "The average is: " << average << endl;
    
}// compute average.

void computethegrade()
{
    if (average > 100)
    {
        cout << "invalid average for grade please try again." << endl;
    }
    else if(average >= 90 && average <= 100)
    {
        cout << "Letter grade is A!" << endl;
    }
    else if (average >= 89 && average <= 80)
    {
        cout << "Letter grade is B" << endl;
    }
    else if (average >= 79 && average <= 70)
    {
        cout << "Letter grade is C" << endl;
    }
    else if (average >= 79 && average <= 60)
    {
        cout << "Letter grade is D" << endl;
    }
    else
    {
        cout << "Letter grade is F!" << endl;
    }
    
    cout << endl;
    
}// End compute the grade.

void classaverage()
{
    class_average = sum_of_test_scores / test_scores;
    cout << "The class average is: " << class_average << endl;

}// End class average.

void classstandarddeviation()
{
    standard_deviation = sqrt(pow(exam_scores[count] - average, 2.0) / SIZE);
    cout << "The standard deviation is: " << standard_deviation << endl << endl;
    
}// End class standard deviation. 


File:

Amy Adams
10111
97 86 78 95
Ben Barr
20222
89 81 73 87
Carla Carr
30333
79 71 63 77
Don Davis
40444
69 62 58 67
Edna Eaton
50555
63 51 62 48
1. I need some how to use the structure which I have made but don't know how to use.

What structure? I don't see any structure being defined anywhere in your program.
2. I am having problems using my different variables arose all my functions.

It looks like you'll need to create functions that have parameters and possibly return values, instead of functions that return nothing (void) and take no parameters.

I suggest you start by defining a structure that will hold the required information, student name, student id, a vector or array of test scores, along with any other required field from your instructions. Then create a vector or array of this structure that you pass to and from your required functions.

@Jib The structure is inside their main function. Though if they wish to access it anywhere else they need to put it in the global space.
Line 79-84 is my structure. I am technically not a lode to use global variables.
A structure isn't a variable, it is more like a description of what a variable will look like.

If you do this:
1
2
3
4
5
6
struct studentinfo
{
    int student_grade_records;
    double average_exam_score;
    int grade;
};


 
    studentinfo fred;


Here fred is a variable, or an object of type studentinfo. The structure can be moved to the global space without breaching the rule forbidding the use of global variables.
Last edited on
Line 79-84 is the definition of a struct. It is not a variable and should be visible in the scope in which you want to use it, which is presumably larger than the scope of main's body.
Topic archived. No new replies allowed.