Storing vectors in vectors?

So I'm new to coding and I'm having difficulty with this homework.
Basically the hw is an athletic department wants a program that calculates the area of basketball courts. But I first have to ask how many courts there are, and then from there ask for the name, length, and width of the courts.
After that, I need to list the courts and their length and width like this:

BASKETBALL COURTS AREA REPORT
BasketBall Length Width
Magic Arena 26 30
Mitchell Center 25 30

as well as asking for the total area and listing the largest one out of all of them, like this:

The total area covered by all of the them together is : 1530
The largest basketball court is Magic Arena : 780

I'm aware that I need to use vectors and a for loop, because I have no idea how many courts there are going to be, but I don't know to refer to them later and list them in the format above. If I knew how many there were this would be a lot easier, and my professor barely even covered vectors. I've searched all over the place and don't know where to start with this. I'm kind of thinking that I need store each vector produced by the for loop into a larger vector, but I have no idea how to find do that and I haven't found a problem similar to this any where online. Any help and tips would be greatly appreciated.
Last edited on
You don’t actually need a vector/array/etc for this homework.

You only need:
  • the current (input) name, width, height, area
  • the largest name, width, height, area
  • the running sum of the areas entered so far

Hope this helps.
So how would I print out the user input later? Like how would I store that information?
if you know about user defined types, you do it like this:

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
struct courts
{
   string name;
   int length;
   int width;
   int area;  //notice how I thought ahead and put in the answer you need too. 
};

vector<courts> courtsdata; 

typically you deal with this by
courts tmp;
filevar>> tmp.name;
filevar>> tmp.length;
filevar>>tmp.width;

tmp.area = tmp.length*tmp.width; //thinking ahead again, compute this while its in-hand rather than do it later.  you know you need it. 

courtsdata.push_back(tmp);  //this is how you handle unknown # of input with vectors. 

…

and later
courtsdata[index].name; //where index is 0 to < courtsdata.size() like an array
 eg 
for(int ii = 0; ii < courtsdata.size(); ii++)
cout << courtsdata[ii].name <<" " << courtsdata[ii].area << endl;


that should get you pretty close. I left you plenty to do... you need to find the largest area and glue it all up in working code, read your file, all that stuff...
Last edited on
So this is how I decided to approach it.

struct courts{
string name;
int length;
int width;
int area;
};
int main (){
int numberofCourts;
cout << "Number of courts: ";
cin >> numberofCourts;

vector<courts> courtdata;
string name;
int length;
int width;
int area;

for (int i=1;i<=numberofCourts;i++){

cout << "Court name: ";
cin >> name;
courtdata.push_back(name);

cout << "Length: ";
cin >> length;
courtdata.push_back(length);

cout << "Width: ";
cin >> width;
courtdata.push_back(width);

area=length*width;
courtdata.push_back(area);

}
}

I'm gettin an error with the coufrtdata.push_back though, it's saying there is no matching member function for call to push_back
Last edited on
it does not work that way. push_back puts an entire item of the <type> used in vector. you cannot say vector<int> x; x.push_back("no good") … that is what you are doing (mismatched types -- in this cheezy example shove a string into an int). You must make the temporary and insert that into the vector (you can reuse temp though).

I showed this in mine, look at it a second time, and realize what the temp is doing for you.

the type of item in your vector is the user defined type "courts" (struct, class, and typedef keywords can make user defined types). think of "courts" like int or double...
Last edited on
Topic archived. No new replies allowed.