Passing user-input array size to constructor

Im writing a simple program that records the quiz scores of students. It starts with asking the user the number of students in the class. I use this number to make an object array of that size.

However, there seems to be an error. First, here is a snippet of my code:

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
#include <iostream>

using std::cout;
using std::cin;
using std::endl;
using std::flush;
using std::string;

class Quizdata{
private:
    string name;
    double total;
    int number;

public:
    Quizdata(int SIZE)
    {
        total = 0;
        number = 0;

        for(int i=0; i<SIZE; i++)
        {
            cout << "Enter in name of student " << i+1 << ": ";
            cin >> name;
        }

    }

    int add_quiz(int score)
    {
        total+=score;
        number++;
        cout << "total so far is: " << total << endl;
    }

    string get_name()
    {
        return name;
    }

    int get_total_score()
    {
        return total;
    }

    int get_average_score()
    {
        return total/number;
    }
};


void printresults(Quizdata[], int); // Prototype function

int main()
{
    int SIZE, quizzes, score;

    cout << "Enter in number of students in class: ";
    cin >> SIZE;
    Quizdata student[SIZE](SIZE); // This is supposed to make student object
                                  // array of size SIZE and pass SIZE
                                  // to the constructor
 


When I compile this, I get the following error: variable sized object 'student' may not be initialized.

What am I doing wrong here? When I change the constructor to a default one (without any parameters and the for-loop inside removed) and remove the variable argument passed in main, then the code works fine.
Last edited on
closed account (48T7M4Gy)
line 57: you need to initialize SIZe. eg int SIZE = 10;

Also,
http://www.cplusplus.com/doc/tutorial/arrays/
Last edited on
You can't have variable sized arrays. You can however have variable sized dynamically allocated arrays.
You can't have variable sized arrays. You can however have variable sized dynamically allocated arrays.


Then how come my variable size array works when I make the constructor default and remove the argument of 'SIZE' passed in main?

like so:

Quizdata student[SIZE]

^ this works when I compile.
Last edited on
Looks like you're trying to initialize a Quizdata 2D array object, which doesn't exist in your class as an explicit constructor. You've got one explicit constructor that takes an int parameter.

EDIT: Disregard. I mistook the brackets for parentheses.
Last edited on
Then how come my variable size array works when I make the constructor default and remove the argument of 'SIZE' passed in main?


If you remove the 'SIZE' variable passed to the array size then... well, you don't have an array.
If you meant from the constructor, then it shouldn't work then as you still have a variable sized array that is not dynamically allocated.
No, I still have an array of objects called 'student'. If I removed the 'SIZE' in parenthesis (i.e. the argument passed to the constructor), and the parameters in the constructor, it works fine. If I keep it, I get the errors.

So its this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    Quizdata(int SIZE)
    {
        total = 0;
        number = 0;

        for(int i=0; i<SIZE; i++)
        {
            cout << "Enter in name of student " << i+1 << ": ";
            cin >> name;
        }

    }

...
...
...

cout << "Enter in number of students in class: ";
    cin >> SIZE;
    Quizdata student[SIZE](SIZE);


verses, this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    Quizdata()
    {
        total = 0;
        number = 0;
    
       cout << "Enter name of student: ";
       cin >> name;

    }

...
...
...

cout << "Enter in number of students in class: ";
    cin >> SIZE;
    Quizdata student[SIZE];



The SECOND works, the FIRST doesn't. I just want to know why the FIRST doesn't work. You said its because I have a variable sized array, but I have a variable sized object array in both cases, so this doesn't make sense to me.
Last edited on
Neither should work at all unless you're using a compiler that supports variable sized arrays. And even the you can't declare an array of objects without a default constructor like that.

Here are your options then:
1.) Array of pointers : Quizdata *pStudents[SIZE]
2.) Vector : std::vector<QuizData> students(SIZE, QuizData(SIZE));
Last edited on
Im still confused but thanks for the help.

What I have right now (object array) seems to be working and has always worked for me so I'll stick with that. You keep saying it shouldn't work but I've seen this in many other programs so i dont know what you are talking about.

But thanks anyways.

Marked as resolved.
As I said before it's probably all dependent on your compiler. That code will not compile under Visual Studio.
Topic archived. No new replies allowed.