Populating arrays from files.

Pages: 12
@Chervil

So I think I am getting closer and wonted you to take a look at what I have. It still is not working but I have the whole thing written and have been working on it I have seem to hit a wall tho. can you help me out?

When I Run it I get

5 5 5 5
5 5 5 5
5 5 5 5
5 5 5 5
5 5 5 5
Letter grade is F!
Class average is: 0.4


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
163
164
165
166
//  main.cpp
//  Program 8
//  Created by William Blake Harp on 7/16/14.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <vector>

int size;
int sum_of_test_scores;
double average;
double class_average;
int r = 0;


using namespace std;

int main()
{
    ifstream inputFile;
    
    string data = "Student Data.txt";
    
    // Open the file
    inputFile.open("data.c_str");
    
    //2D Array.
    const int students = 5;
    const int test_scores = 4;
    int s[students][test_scores];
    
    //Arrays on the heap.
    int* arrayNames = nullptr;
    arrayNames = new int[size];
    
    int* arrayID = nullptr;
    arrayID = new int[size];
    
    int* arrayAverage = nullptr;
    arrayAverage = new int[size];
    
    int* arrayLetterGrade = nullptr;
    arrayLetterGrade = new int[size];
    
    // Populating 2D array test_scores.
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < test_scores; j++)
        {
            cin >> s[i][j];
        }
    }
    
    // Displaying 2D array test_scores.
    for (int i = 0; i < r; i++)
    {
        for(int j = 0; j < test_scores; j++)
        {
            cout << s[i][j];
        }
        cout << endl;
    }
    
    // Populating array on the heap arrayNames
    for (int i = 0; i != size; i++)
    {
        cin >> arrayNames[i];
    }
    
    // Displaying array on the heap arrayNames
    for (int i = 0; i != size; i++)
    {
        cout << arrayNames[i];
    }
    
    // Populating array on the heap arrayID
    for (int i = 0; i != size; i++)
    {
        cin >> arrayID[i];
    }
    
    // Displaying array on the heap arrayID
    for (int i = 0; i != size; i++)
    {
        cout << arrayID[i];
    }

    // Populating array on the heap arrayAverage
    for (int i = 0; i != size; i++)
    {
        cin >> arrayAverage[i];
    }
    
    // Displaying array on the heap arrayAverage
    for (int i = 0; i != size; i++)
    {
        cout << arrayAverage[i];
    }

    // Populating array on the heap arrayLetterGrade
    for (int i = 0; i != size; i++)
    {
        cin >> arrayLetterGrade[i];
    }
    
    // Displaying array on the heap arrayLetterGrade
    for (int i = 0; i != size; i++)
    {
        cout << arrayLetterGrade[i];
    }

    do {
        // Average
        sum_of_test_scores += test_scores;
        average = sum_of_test_scores / 4;
    
        for(int i=0; i<students; i++)
        {
            for(int j=0; j<test_scores; j++)
                cout << s[students][test_scores] <<" "; cout<<endl;
        }
    
        // Geting a letter grade.
        if (average >= 90)
        {
            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 (average < 60);
        {
            cout << "Letter grade is F!" << endl;
        }
    } while (students < 5);
    
    // Geting the class average
    class_average = (++average) / 5;
    cout << "Class average in: " << class_average << endl;

    // Close the file.
    inputFile.close();
    
    // releasing memory block on the heap.
    delete [] arrayNames;
    arrayNames = 0;
    delete [] arrayID;
    arrayID = 0;
    delete [] arrayAverage;
    arrayAverage = 0;
    delete [] arrayLetterGrade;
    arrayLetterGrade = 0;
    
    return 0;
}// End Code. 
As you know, cin and cout are used for input and output respectively, to/from the console. cin is a type of input stream. When you use a file for input, then you use an ifstream - that is another type of input stream.

In most cases the streams share common behaviour.
for example you can do this such things as cin >> x, or getline(cin, y)

or equally, you can do something like this,
ifstream inputFile("Student Data.txt");
then inputFile >> x, or getline(inputFile, y)

Notice how cin can simply be replaced by inputFile, that's because both are input streams. I hope this helps a little, I'm trying to illustrate at least a little bit, how these things fit together.

Last edited on
No that is very helpful I am going to see what I can do with this.
Topic archived. No new replies allowed.
Pages: 12