arrays must be constant sized in standard c++, so n is not legal. many compilers support it anyway, but it isnt safe to rely on.
on top of which, making an array of size 1 is pointless.
arrays do NOT grow in c++. you are out of bounds and causing all kinds of hidden problems with this code (it likely damages nearby variables, which in turn could indeed cause it to hang or run forever or anything else).
you want a vector: those can resize on demand.
vector<int> area;
and to assign it in your loop, instead of area[n] = use
area.push_back((vertice2x-vertice1x)*(vertice2y-vertice1y));
and to print it you can use area.size() in a for loop like your n based loop, or you can use a ranged based for loop here and skip that extra typing.
look in the reference section here on vectors for more info... this is just the starting point.
c++ is zero based. so array[0] is the first item in an array or vector, not [1]
there may be other issues, but those are the most immediate ones.
more info: C++ is a mixed level language, meaning it is capable of low level things, and arrays in c++ are low level things. An array is effectively the starting point into a location in memory with a built in byte-size so it can move from one complete item to another (that is, for a 4 byte integer, it moves 4 bytes for you when you use [0] or [1], [1] is automatically the location+4 in that example). That is all it can do. You can't assign arrays to each other, it does not know how big itself is, it can't grow to hold more stuff, its just a raw block of memory in bytes (block meaning its all sequential bytes of memory). Most c++ coders and coders coming from purely high level languages use a vectors which can do these things (resize, assignment, know own size, more). Arrays are, take your pick: { a left over from C, an experts-only feature, a feature used in the 90s, dangerous and not used much anymore}. All those are true from one point of view or another :). I am older, and I fall back to arrays at times when I should not in quick home-code, and you will see examples of arrays everywhere in code, even when its not the best tool. Old habits die hard.
Roughly, then:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include<vector>
int main()
{
int vertice1x,vertice1y,vertice2x,vertice2y;
vector<int> area;
do
{
cin>>vertice1x>>vertice1y>>vertice2x>>vertice2y;
area.push_back((vertice2x-vertice1x)*(vertice2y-vertice1y));
}while(vertice2x>vertice1x || vertice2y>vertice1y);
for(int x=0; x<area.size(); x++)
{
cout<<area[x]<<endl;
}
return 0;
}
|