I am seeking some guidance as to where I went wrong with my program. I am supposed to write a program that lets a user input 10 integer values into a vector ad then have the highest and lowest values stored in the vector displayed. This needs be completed by using two return functions that take the vector in as a parameter and return the values. My program will execute but will crash after inputting the first value. Please help! Thank you in advance!
//find the highest and lowest values in a vector.
#include <iostream>
#include <vector>
usingnamespace std;
int main ()
{
vector<int> values; //numbers that are entered
int count;
//Ask for entry of numbers into the vector
for (count= 0; count < 10; count++)
{
cout << "Please enter a number "
<< (count + 1) << ": ";
cin >> values[count];
}
int highest;
highest = values[0];
for (count = 1; count < 10; count++)
{
if (values[count] > highest)
highest = values[count];
}
//display the highest number in the vector
cout << "The highest number out of the ten entered is: " <<
highest << endl;
//determine the lowest number out of the ten entered
int lowest;
lowest = values[0];
for (count = 1; count < 10; count++)
{
if (values[count] < lowest)
lowest = values[count];
}
//display the lowest number in the vector
cout << "The lowest number out of the ten entered is: " <<
lowest << endl;
return 0;
}
cin >> values[count];
Here your trying to access a vector of size 0. You need to either give the vector a size eg values.resize(10) or use the push_back() method