Expression must have constant value

Hello, as the title says, I keep getting this error starting in line 14 and beyond. Int n gives me the error "expression must have constant value". I've researched this error message, but need a nudge in the right direction.

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

using namespace std;

int main(){

int n, i, j;

cin >> n;

string first[n], last[n];

float gpa[n];

for(i = 0; i < n; i++){

cin >> last[i] >> first[i] >> gpa[i];

}

for(i = 0; i - n < 0; i = i + 1){

for(j = 0; j <= i; j = j + 1){

if(last[i] < last[j]){

string ftemp = first[i];

string ltemp = last[i];

float temp = gpa[i];

first[i] = first[j];

last[i] = last[j];

gpa[i] = gpa[j];

first[j] = ftemp;

last[j] = ltemp;

gpa[j] = temp;

}

else if(last[i] == last[j] && first[i] < first[j]){

string ftemp = first[i];

string ltemp = last[i];

float temp = gpa[i];



first[i] = first[j];

last[i] = last[j];

gpa[i] = gpa[j];

first[j] = ftemp;

last[j] = ltemp;

gpa[j] = temp;

}

}

}

cout << endl;

for(i = 0; i < n; i++){

cout << last[i] << " " << first[i] <<" " << gpa[i] << endl;

}

return 0;

}
Last edited on
The compiler wants the n variable to be a constant (e.g. const int n = 20;) since you're using n to size arrays.

Since you're actually not going to know the size until the user enters it, you need to dynamically allocate the array with new- http://www.cplusplus.com/doc/tutorial/dynamic/ (and later deallocate with delete)

or - you use a vector instead of array.
http://www.cplusplus.com/reference/vector/vector/
Topic archived. No new replies allowed.