Variable Length Array of Non-POD Element Type 'string'

I am experiencing an unusual error for me. The full error message my IDE is giving me is: Variable Length Array of Non-POD Element Type 'string'('aka' basic_string <char, char_traits<char>, allocator<char> >')
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
#include <iostream>
#include <string>
using namespace std;
int main()
{
    int size;
    int scores[size], i, sum = 0, average;
    string names[size]; // The error is located on this line.
    cout << "Enter the number of students.";
    cin >> size;
    cout << "Enter each score.";
    for (i = 0; i < size; i++)
        cin >> scores[size];
    cout << "Enter the first name of each student";
    for (i = 0; i < size; i++)
        cin >> names[size];
    for (i = 0; i < size; i++)
        sum += scores[size];
    average = sum / size;
        for (i = 0; i < size; i++)
        {
            if (scores[size] > average)
                cout << names[size] << endl;
        }
    return 0;
}
C++ standard requires that arrays use either an integer literal or a integer constant when declaring its size. Use <vector> instead:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(){

	int size;
	vector<int> scores;
	vector<string> names;
.
.
.


Then use the member functions scores.push_back(elementToBeAppended) and names.push_back(elementToBeAppended) to append new elements to each <vector>. You will want to keep the variable int size to keep track of how big the vectors get, and you can call the individual elements just like you do for normal arrays.

Additional reading:
http://www.cplusplus.com/reference/vector/vector/
Last edited on
closed account (Dy7SLyTq)
vector is called a container. there are many containers to make life easier that were a pain to do in c. some of these are
queue <== A queue
dequeue <== a double ended queue
list <== a singly (i think... it might be doubly) linked list
bitset <== i think thats the name. ive never used it before so idk what you use it for
there are more i think. this site can teach them to you
there are also more in boost
Interesting. Arrays are called containers as well.

http://www.cplusplus.com/reference/stl/
closed account (Dy7SLyTq)
i guess it can be considered a built in container. but its talking about a different array there. ie array<int, 10> MyArray;
Yeah, I was just reading about the difference. I wish my school's compiler was up to date... :-/
closed account (Dy7SLyTq)
what kind of compiler is it?
g++ variant. They use it on a Debian shell, and it works with their listexec program. I ran a script to find out which C++ standard it was using, and it was pre 98.
Last edited on
Topic archived. No new replies allowed.