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> >')
#include <iostream>
#include <string>
usingnamespace 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>
usingnamespace 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.
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
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.