Help with using user input values of an array with constructors

I'm having trouble understanding what the issue is with my code. The error messages popping up state that, on line 53, the variable on the left of '.' needs to be of type class/struct. But, haven't I already initialized "skateboardCollection" as my class type "Skateboard"? My original prompt for the program is as follows:

"You should have a class called Skateboard where Skateboard has 3 public member variables (refer to Ex 27 for details). You don’t need the previous code for the main function. Remove it and make a clean main function to start with.

1) Add 1 more public double member variable called width.

2) Modify print member function to print each element of the Skateboard class, now including width.

3) Write 2 constructors as follows:

default constructor (has NO input parameter) with member initialization list that initializes the brand and model with “NA”, and length and width to -1.0. Since this uses a member initialization list, there shouldn’t be any statements inside the body. 2nd constructor overloaded with 4 parameters for the skateboard brand, model, length, and width, in that order. The constructor will initialize all of the member variables and will accept values for these parameters. Also this should have an empty body; the assignments should be done in the header."

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

class Skateboard {
public:
	Skateboard();
	Skateboard(string b, string m, double l, double w); 
	void print() {
		cout << "brand: " << brand << " ";
		cout << "model: " << model << " ";
		cout << "length: " << length << " ";
		cout << "width: " << width << " ";
	}
	bool isLongBoard() {
		if (length > 28)
			return true;
		else
			return false;
	}

private:
	string brand, model;
	double length, width;
};

	Skateboard::Skateboard() {
		brand = "NA";
		model = "NA";
		length = -1.0;
		width = -1.0;
		return;
	}

	Skateboard::Skateboard(string b, string m, double l, double w) {
		brand = b;
		model = m;
		length = l;
		width = w;
	}

int main() {
	string b, m;
	double l, w;
	int i;
	Skateboard skateboardCollection[4];

	for (i = 0; i<2; i++) {
		cin >> b;
		cin >> m;
		cin >> l;
		cin >> w;
		skateboardCollection.Skateboard(b, m, l, w);
	}

	for (i = 0; i<2; i++) {
		skateboardCollection[i].print();
		if (skateboardCollection[i].isLongBoard() == true)
			cout << "isLongBoard: 1 ";
		else
			cout << "isLongBoard: 0 ";
	}

	system("pause");
	return 0;
}
You should have a class called Skateboard where Skateboard has 3 public member variables

Does this mean
1
2
3
public:
	string brand, model;
	double length, width;

?

If so, you can do
1
2
3
4
5
6
7
for (i = 0; i<2; i++) {
		cin >> skateboardCollection[i].brand;
		cin >> skateboardCollection[i].model;
		cin >> skateboardCollection[i].length;
		cin >> skateboardCollection[i].width;
		//skateboardCollection.Skateboard(b, m, l, w);
	}
Last edited on
Topic archived. No new replies allowed.