inputting into double array with set size is looping forever

I have a line that says how big two arrays should be and to input x1 and y1 coordinates but it keeps looping forever...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 //connect dots to make triangles and calculate area.

#include <iostream>
using namespace std;
int main()
{
    int triangles;
    double x[triangles];
    double y[triangles];
    cin>>triangles;
    //cout<<"There were "<<triangles<<" triangles ";
    double det = 0.0;
    for (int i=0; i<triangles; i++)
    {
        cin>>x[i]>>y[i];
        cout<<"in loop i = "<<i<<" ";
        if(i>=triangles){break;}
    }
    
}


input:
15
3841 451
9202 2364
9565 3410
9776 5234
8911 7387
7850 9101
7121 9466
5360 9575
3164 9143
2632 8941
2045 8472
834 7464
54 4870
178 3833
3141 977

output: in loop 1 in loop 2 in loop 3...
int triangles;
double x[triangles];
double y[triangles];
cin>>triangles;


Your arrays are sized with the garbage value on line 7.

Not the revised value on line 10.

Maybe
int triangles;
cin>>triangles;
double x[triangles];
double y[triangles];


But note that this isn't legal C++ either.

Use a std::vector instead.
Last edited on
Topic archived. No new replies allowed.