Program Ends Before I get chance to input.

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
#include <iostream>
using namespace std;
struct bessie {
    int speed;
    int time;
} a[1000];
struct elsie {
    int speed;
    int time;
} b[1000];
int main () {
    int n, m;
    cin>>n>>m;
    int diff [1000001];
    int aposition[1000001];
    int bposition[1000001];
    int total=0;
    for (int i=0; i<n; i++) {
        cin>>a[i].speed>>a[i].time;
        total+=a[i].time;
    }
    for (int i=0; i<m; i++) {
        cin>>b[i].speed>>b[i].time;
    }
    int now=1;
    aposition[0]=0;
    for (int i=0; i<n; i++) {
        for (int j=now; j<=a[i].time; j++) {
            aposition[j]=aposition[j-1]+a[i].speed;
            now+=a[i].time+1;
        }    
    }
    now=1;
    bposition[0]=0;
    for (int i=0; i<m; i++) {
        for (int j=now; j<=b[i].time; j++) {
            bposition[j]=bposition[j-1]+b[i].speed;
            now+=b[i].time+1;
        }    
    }
    for (int i=0; i<=total; i++) {
        diff[i]=aposition[i]-bposition[i];
    }
    int count=0;
    int temp=0;
    int last=0;
    for (int i=0; i<=total; i++) {
        if (diff[i]<0)
        temp=-1;
        if (diff[i]>0)
        temp=1;
        if (temp!=last)
        count++;
        last=temp;
    }
    cout<<count;
}

Program ends execution before I can input.
I think you've probably blown the stack. Use vectors or make your arrays static.
I'm new to c++, but I did not see a system("pause"); or a return statement in int main()
I'm new to c++, but I did not see a system("pause"); or a return statement in int main()

https://stackoverflow.com/questions/19293642/c-why-main-works-with-no-return-value

Using any system() command can be dangerous.
http://www.cplusplus.com/forum/articles/11153/

https://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong
Last edited on
Stack overflow, you really blew up the program's stack. I debugged your program in VS 2019 and it really crapped its pants.

You are requesting memory for over 3 million int variables. IIRC memory for C style arrays is allocated when a program starts up, before main begins executing.

Reduce the size of the 3 arrays, lines 14-16, to much smaller number of elements, say [1001], and the program doesn't crash.

Try adding some text prompts, you are blindly requesting input without letting the user know what is going on. Being confronted with a continuing blinking cursor endlessly is a bit frustrating.

Sure, YOU know what is going on, but blank requests for input is still counter-intuitive. With some text before the std::cin inputs would help to detect any possible logic errors.
Last edited on
    int diff [1000001];
    int aposition[1000001];
    int bposition[1000001];

Your default stack size is 1MB (it seems to be the most commonly cited value for windows).
These three arrays take up 12MB, if you assume the minimum size of an int as being 4 bytes.

You can change the stack size, but it's a fiddle to do it.

Picking 'impossibly' large sizes in the hope that your user won't enter values for cin>>n>>m; that won't overflow your arrays is a poor defence strategy.

> struct bessie {
> struct elsie {
These have exactly the same members, with the same names.
If they really are the same, you only need one structure declaration.


Topic archived. No new replies allowed.