Too many initializer values

Feb 5, 2020 at 8:49pm
My current code right now is supposed to let you pick what class you want and display it and show the starting stats. However, when I try to assign the stats lines 29,37,45 say that there are too many values and '=' cannot convert from 'initailizer list' to 'int'. What am I missing here?

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

class start
{
public:
	void strartingclass();
	void getclass();
private:
	int starting_class, intelligence, strength, agility;
	string attributes[3] = { "intelligence","strength","agility" };
	int attribute_points[];
};

void start::strartingclass()
{
	cout << "Pick a class to choose from: " << endl;
	cout << "-------------------------------" << endl;
	cout << "1. Mage / 2. Ninja / 3.Barbarian" << endl;
	cin >> starting_class;
}

void start::getclass()
{
	switch (starting_class)
	{
	case 1:
		cout << "Mage" << endl;
		attribute_points[3] = { 6,2,3 };
		for (int i = 0; i < 3; i++)
		{
			cout << attributes[i] << ": " << attribute_points[i] << endl;
		}
		break;
	case 2:
		cout << "Ninja";
		 attribute_points[3] = { 4,4,6 };
		for (int i = 0; i < 3; i++)
		{
			cout << attributes[i] << ": " << attribute_points[i] << endl;
		}
		break;
	case 3:
		cout << "Barbarian";
		 attribute_points[3] = { 2,7,3 };
		for (int i = 0; i < 3; i++)
		{
			cout << attributes[i] << ": " << attribute_points[i] << endl;
		}
		break;
	
	}
}

int main()
{
	start classes;
	classes.strartingclass();
	cout << "Class chosen: "; 
	classes.getclass();
}
Feb 5, 2020 at 8:52pm
You can't assign to an array like that (lines 29, 37, 45).

First, line 12: Your array should have a length. Make it 3.
int attribute_points[3];

Next, instead of the current syntax, one option is to assign to each element, something like:
1
2
3
4
5
int mage_attr_points[] = { 6, 2, 3 };
for (int i = 0; i < 3; i++)
{
    attribute_points[i] = mage_attr_points[i];
}

etc. for each type.

Alternatively, you can use a vector, which allows dynamically setting/assigning to it.
 
vector<int> attribute_points;

...
 
attribute_points = {6, 2, 3};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Example program
#include <iostream>
#include <vector>
using std::vector;

int main()
{
    vector<int> attribute_points;
    
    // .....
    bool condition = true; // etc.
    if (condition)
    {
        attribute_points = {6, 2, 3};
    }
}
Last edited on Feb 5, 2020 at 8:55pm
Feb 5, 2020 at 8:59pm
By that point in the code, attribute_points already exists, so you can't create it for the first time, because it already exists.

Why are you using arrays? Seriously, C++ has vectors for a reason. You're just asking for trouble using arrays (and indeed, you ran right into that trouble, because the array you created is of size... well, you tell me. What size is this array?

int attribute_points[];



Just use vectors. Especially as a beginner. Vectors are for beginners. Arrays are for people who have no choice.
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
#include<iostream>
#include<vector>

using namespace std;

class start
{
public:
	void strartingclass();
	void getclass();
private:
	int starting_class, intelligence, strength, agility;
	string attributes[3] = { "intelligence","strength","agility" };
        vector<int> attribute_points;
};

void start::strartingclass()
{
	cout << "Pick a class to choose from: " << endl;
	cout << "-------------------------------" << endl;
	cout << "1. Mage / 2. Ninja / 3.Barbarian" << endl;
	cin >> starting_class;
}

void start::getclass()
{
	switch (starting_class)
	{
	case 1:
		cout << "Mage" << endl;
		attribute_points = vector<int>{ 6,2,3 };
		for (int i = 0; i < 3; i++)
		{
			cout << attributes[i] << ": " << attribute_points[i] << endl;
		}
		break;
	case 2:
		cout << "Ninja";
		 attribute_points = vector<int> { 4,4,6 };
		for (int i = 0; i < 3; i++)
		{
			cout << attributes[i] << ": " << attribute_points[i] << endl;
		}
		break;
	case 3:
		cout << "Barbarian";
		attribute_points = vector<int>{ 2,7,3 };
		for (int i = 0; i < 3; i++)
		{
			cout << attributes[i] << ": " << attribute_points[i] << endl;
		}
		break;
	
	}
}

int main()
{
	start classes;
	classes.strartingclass();
	cout << "Class chosen: "; 
	classes.getclass();
}
Last edited on Feb 5, 2020 at 9:00pm
Topic archived. No new replies allowed.