An Error

The compiler gives an error at 19th line: C:\Users\toshiba\Desktop\CPP\Empire\CharacterCreator.cpp variable-size type declared outside of any function

The code is:

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
#include <iostream>
#include <string>
#include <ctime>
using namespace std;

int idno;
int npcquant;
void createnpc ()
{
     cout << "How many npcs to create? ";
     cin >> npcquant;
     }

struct npcdetails
{
       int npc_id;
       string name;
       string surname;
       }people [npcquant];



       void randomname()
       {
           int n;

           string surnames [48] = {"Snow", "Holmes", "Anderson", "Taylor", "Thompson",
           "Hewett", "Miller", "Cook", "Watson", "Parker", "Smith", "Turk", "Horn",
            "Raymond", "Cakes", "Poe", "Wright", "Johnson", "Adams", "Stevens", "Sellers", 
            "Brown", "Silver", "Green", "Carter", "Black", "Williams", "James", "Baker",
             "Pickard", "Hodson", "Chin", "Petrovic", "Hayes", "Lawson", "Olson", 
             "Stewart", "Robinson", "Dixon", "Carlton", "Mendler", "Stone", "White", 
             "Jones", "Shepherd", "Arch", "Morris", "Walker"};
           
           string names [48] = {"John", "Adams", "Bryan", "Brian", "Marcus", "Will",
           "Rob", "Alex", "Arthur", "Earl", "Randy", "Jack", "Eric", "Paul",
           "David", "Ivan", "Craig", "Michael", "Brad", "Andy", "Greg", "Sean",
           "Morgan", "Andrew", "Ronnie", "Isaac", "Luke", "Daniel", "Ryan",
           "Josh", "Lee", "Antony", "Blake", "Charles", "Simon", "Steve", "Pete", "Tim",
           "Duncan", "Oliver", "Ed", "Scott", "Lance", "Oscar", "Mike", "Sam", "Josh",
           "Matt"};
           
           srand (time(0));
           for (idno=0;idno<npcquant;idno++)
           {
               n = rand () % 48;
               people[idno].name = names [n];
               n = rand () % 48;
               people[idno].surname = surnames [n];
               people[idno].npc_id = idno;
               people[idno].npc_id += 1;
               }
}

int main()
{
    int n;
    createnpc();
    randomname();
    cout<< "\n\n";
    for (n=0; n<npcquant; n++)
    {
        cout<<"#";
        if ((n/10) == 0)
        cout<<"0000";
        else if ((n/100) == 0)
        cout<<"000";
        else if ((n/1000) == 0)
        cout<<"00";
        else if ((n/10000) == 0)
        cout<<"0";
        cout<<people[idno].npc_id << " - " <<people[idno].name<<" "<<people[idno].surname;
        cout<<"\n";
        }
        cout<<"\n\n";
        system ("pause");
        return 0;
}


Can anybody help about this error?
Static arrays (such as that one, declared in global scope) can only have a constant size.
I tried to apply dinamic memory issues in tutorial but it didn't work. A lot of errors have been appeared.

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
#include <iostream>
#include <string>
#include <ctime>
#include <new>
using namespace std;

int idno;
int npcquant;
int people;
int * p;
p = &people;
p= new (nothrow) int[npcquant];


void createnpc ()
{
     cout << "How many npcs to create? ";
     cin >> npcquant;
     }

struct npcdetails
{
       int npc_id;
       string name;
       string surname;
       }people;

...
...


Is there any way to use a structure with an array which have a variable size?
If you want to use dynamic allocation, do so inside a function. E.g.:
1
2
3
4
5
6
7
int *p;

int main(){
    //declaration and initialization of var.
    p=new int[var];
    return 0;
}
Topic archived. No new replies allowed.