Crashing on run

I'm working on an exercise that the user puts in doubles one just for as long as they want and if it's the lowest so far, it tells you. If it's the highest so far, it tells you. Problem is, I can't even test the code. Compiler lets me build it with no complaints, but when I go to run it just crashes. I'm not sure what I'm doing wrong. Can someone run this for me and see if it's just my compiler set up?




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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    double x = 0, y = 0;
    vector<double> couple;
    const double highest = *max_element (couple.begin(), couple.end());
    const double lowest = *min_element (couple.begin(), couple.end());

    cout<<"Enter a value: "<<endl;

    while(cin>>y)
    {
        couple.push_back(y);
        if(y<lowest) {cout<<"Smallest number so far."<<endl;}
        else if (y>highest){cout<<"Largest number so far."<<endl;}

        x = y;
    }
    return 0;
}
Last edited on
It's not just you.

I believe the problem is you can't call max_element or min_element on an empty container (which couple is at that point). You will want to calculate those values every loop not before the loop even starts.
Thanks! That coupled with added two numbers to vector to start with did the trick looks like this now.

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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    double x = 0, y = 0;
    vector<double> couple;
    couple.push_back(4);
    couple.push_back(5);

    cout<<"Enter a value: "<<endl;

    while(cin>>y)
    {
        const double highest = *max_element (couple.begin(), couple.end());
        const double lowest = *min_element (couple.begin(), couple.end());

        couple.push_back(y);
        if(y<lowest) {cout<<"Smallest number so far."<<endl;}
        else if (y>highest){cout<<"Largest number so far."<<endl;}

        x = y;
    }
    return 0;
}


Thanks for the help. I'd upvote you if I could lol
I ran this in my complier as I'm having issues with my code as well. My complier runs it however it doesnt perform my code correctly.

As for yours I get an error code 193 not a valid win32 application
Topic archived. No new replies allowed.