Passing vectors through functions causes crash

iv looked over this 10000 times and i dont understand what the problom is,
for some reason if i try to call the vector up in the function trying to make the SVG file the entire thing crashes on me, i could really use some help as i am very very confused

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;
int const MAX = 100;
int const maxhight = 475;
int const maxwidth = 475;
void inputtofile1(vector<int> & Tavgs,int& wide,int Max, double width);
void askforEC( vector<int> Tavgs,vector<int> avgs,int& F,int& D,int& C,int& B,int& A);
void fillmatrix(vector<int> grades1,int matrix[20][20],int& j);
//takes all the grades submited and puts them in the matrix
void extracredit(vector<int> Tavgs);
// shows all the numbers for the test avreges in a star graph
void extracredit3(vector<int> avgs);
//shows all the numbers for the student avgs in a star graph
void extracredit2(int& F,int& D,int& C,int& B,int& A);
//takes all the numbers from the total grades and puts them into star graph
void ind_avg(vector<int> grades1,vector<int> avgs, int avg);
//finds the avg of each individual student
void inputfile(vector<int> & Tavgs,vector<int> grades1, vector<int> avgs,string firstname,string lastname,int& hight,int& F,int& D,int& C,int& B,int& A,int& j,int& wide);
//reads from the txt file all the info and sorts/puts things where they need to go.
void scoretally(int matrix[20][20],int& F,int& D,int& C,int& B,int& A,int& hight);
// figures out how many of each grade there is
void testavg(int matrix[20][20],vector<int> Tavgs,int& hight,int& wide);
//finds the avg score for each test
int main()
{
    int F=0, D=0, C=0, B=0, A=0;
    vector<int> grades1;
    vector<int>avgs;
    vector<int>Tavgs;
    vector<int>number;
    int matrix[20][20];
    int hight= 0,j,Max,wide;
    double width;
    string firstname,lastname;
inputfile(Tavgs,grades1,avgs, firstname, lastname,hight, F, D, C, B, A,j,wide);



askforEC( Tavgs,avgs,F,D,C,B,A);
}
void inputfile(vector<int> & Tavgs,vector<int> grades1, vector<int> avgs,string firstname,string lastname,int& hight,int& F,int& D,int& C,int& B,int& A,int& j,int& wide)//reads from the txt file all the info and sorts/puts things where they need to go.
{
    j=0;
    wide = 0;

    int i, temp = 0;
    int avg;
    int matrix[20][20];
    int numbers,width, Max;
    char numbers1;
    ifstream grades;
    grades.open ("gradeBook.txt");
    if (grades.fail())
    {
        cout << "error1";
    }
    while (grades >> firstname)
    {
    grades >> lastname;
    grades.get(numbers1);
    while (numbers1 != '\n')
    {
    grades.putback(numbers1);
    grades >> numbers;
    grades1.push_back(numbers);
    grades.get(numbers1);
    }
    cout << firstname << " " << lastname << " ";
    fillmatrix(grades1,matrix,j);
    ind_avg(grades1,avgs,avg);
    hight++;
    wide = grades1.size();
 grades1.clear();
    }
    scoretally(matrix,F,D,C,B,A,hight);
    testavg(matrix,Tavgs,hight,wide);
   inputtofile1(Tavgs,wide,Max,width);
}
void ind_avg(vector<int> grades1,vector<int> avgs, int avg) //finds the avg of each individual student
{
    int ammount=0,sum=0,i,temp=0;
    for (i=0;i<grades1.size();i++)
    {
        sum = sum + grades1[i];
        ammount++;
    }
    avg = sum / ammount;
    avgs.push_back(avg);

}
void fillmatrix(vector<int> grades1,int matrix[20][20],int& j) //takes all the grades submited and puts them in the matrix
{
int i;
    for (i=0;i<grades1.size();i++)
    {
        matrix[j][i] = grades1[i];
        cout << matrix[j][i] << " ";
    }
    cout << "\n";
    j++;
}
void scoretally(int matrix[20][20],int& F,int& D,int& C,int& B,int& A,int& hight) // figures out how many of each grade there is
{
    int j,i;
    for (j=0;j<hight;j++)
    {
    for (i=0;i<10;i++)
    {
    if (matrix[j][i] < 60)
        {F++;}
    else if ( 59 < matrix[j][i] && matrix[j][i] < 70)
    {D++;}
    else if ( 69 < matrix[j][i]  && matrix[j][i] < 80)
    {C++;}
    else if ( 79 < matrix[j][i] && matrix[j][i] < 90)
    {B++;}
    else if (89 < matrix[j][i] <= 100)
    {A++;}
    }
    }
cout << A << " " << B << " " << C << " " << D << " " << F << " ";
}
void testavg(int matrix[20][20],vector<int> Tavgs,int& hight,int& wide) //finds the avg score for each test
{
    int sum=0,i,j,ammount=0,avg;
    for (i=0;i<wide;i++)
    {
        for (j=0;j<hight;j++)
        {
         ammount++;
         sum = sum + matrix[j][i];
        }
    avg = sum / ammount;
    Tavgs.push_back(avg);
    }
}

void inputtofile1(vector<int> & Tavgs,int& wide,int Max, double width)
{
int movement1 = 20, movement2=60 ,i,spred = 10;
wide=10;
width = (static_cast<double>(wide) /maxwidth) * 1.5;
cout << " bob" << wide << " " << width << " ";
 ofstream file;
 file.open("file1.svg");
file << "<?xml version=\"1.0\" standalone=\"no\"?>\n";
file << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n";
file << "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd/%22%3E/n";
file << "<svg width=\"500\" height=\"500\"";
file << " xmlns=\"http://www.w3.org/2000/svg/%22%3E/n";
cout << wide << endl;
for (i=0;i<wide;i++)
{
file << "<rect x=\"" << spred << "\" y=\"" << (475-Tavgs[i]) << "\" width=\"" << width << "\" height=\"" << Tavgs[i] << "\" ";
file << "style=\"fill:orange; \"/>\n";
spred = spred + movement2;
}
file << "<line x1=\"0\" y1=\"475\" x2=\"" <<wide * 65 << "\" y2=\"475\" style=\"stroke: purple;stroke-width:2\"/>\n";
file << "<line x1=\"" <<wide * 65 << "\" y1=\"475\" x2=\"" <<wide * 65 << "\" y2=\"20\" style=\"stroke: purple;stroke-width:2\"/>\n";
file << "</svg>";
}
void extracredit(vector<int> Tavgs)
{
  int i,j;
for(i=0;i<Tavgs.size();i++)
{
    for (j=0;j<Tavgs[i];j++)
    {
        cout << "*";
    }
    cout << endl;
}

}
void extracredit2(int& F,int& D,int& C,int& B,int& A)
{
  int i,j;
  cout << endl;
cout << "A's: ";
for(i=0;i<A;i++)
{
    cout << "*";

}
cout << endl;
cout << "B's: ";

for (i=0;i<B;i++)
    {
    cout << "*";
    }
    cout << endl;
    cout << "C's: ";

for (i=0;i<C;i++)
    {
    cout << "*";
    }
    cout << endl;
    cout << "D's: ";

for (i=0;i<D;i++)
    {
    cout << "*";
    }
    cout << endl;
    cout << "F's: ";

for (i=0;i<F;i++)
    {
    cout << "*";
    }
    cout << endl;

}
void extracredit3(vector<int> avgs)
{
  int i,j;
for(i=0;i<avgs.size();i++)
{
    for (j=0;j<avgs[i];j++)
    {
        cout << "*";
    }
    cout << endl;
}

}

void askforEC( vector<int> Tavgs,vector<int> avgs,int& F,int& D,int& C,int& B,int& A)
{
    char awnser;
    char awnser2;
    cout << "would you like to see one of the graphs here? \n";
    cin >> awnser;
    if (awnser =='Y' or 'y')
    {
        cout << "Cool! Which one do you want to see?:\n";
        cout << "1: test avgreges?\n";
        cout << "2: student avreges?\n";
        cout << "3: number of each grades total\n";
        cin >> awnser2;
        switch (awnser2)
        {
        case '1':
            {
            extracredit(Tavgs);
            break;
            }
        case '2':
            {
            extracredit3(avgs);
            break;
            }
        case '3':
            {
            extracredit2(F,D,C,B,A);
            break;
            }
        default:
            {
                cout << "not a valid number sorry.";
            }
        }
    }
    else
    {
    cout << "bye!";
    }
}


the file "gradeBook.txt" is just a little txt document with this in it

test_ line_ 10 20 30 40 50 60 70 80 90 100
Price Betty 40 50 60 70 60 50 40 30 60 90
Good John 60 70 80 90 50 60 90 90 100 90
Smith Charles 70 80 90 60 70 60 80 90 90 90
Spangenberg Ward 70 70 80 90 70 80 90 80 70 60

Last edited on
fix your syntax errors. This shouldn't even compile.
for example
if (awnser == 'Y' or 'y') should be if (awnser == 'Y' || awnser == 'y')
Last edited on
There is a lot of code here for people to check through, it would be a good idea to check the vectors size at different points and see if it is what you expect etc. (Either with debugging tools or cout statements). Will point out a few things I saw.

1
2
void ind_avg(vector<int> grades1,vector<int> avgs, int avg)
void testavg(int matrix[20][20],vector<int> Tavgs,int& hight,int& wide) 

In functions like these you are pushing values into the avgs/Tavgs vectors. However you are passing the vectors by value and not by reference so all of the values will be lost as soon as it exits the function.

if (awnser =='Y' or 'y')

Isn't valid code, it should be
if (awnser =='Y' || awnser == 'y')
ok i fixed the awnser part, how do i fix the vector functions, do i pass them by refrence?
In this case yes, in the same way you have done some other parameters you would write void ind_avg(vector<int> grades1,vector<int> &avgs, int avg) instead of what you have.
Last edited on
ok let me try dont go anywere
Please read up on pass-by-reference and pass-by-value and apply the wisdom you learn from doing so to your program.

You might also want to break the program up into smaller testable parts rather than throwing everything together in one untested mess.
well i was able to fix it, thank you very much for helping me, i might need help getting the SVG file to work but that should be to hard
also, i know what pass by reference and pass by value are, i just thought that vectors were automatically pass by reference
Topic archived. No new replies allowed.