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.
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...
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...