# Am I Using Vectors Wrong?

1. Nov 19, 2009

### Lancelot59

I'm currently learning C++, and I'm trying to use vectors and some simple loops to find the largest/smallest values out of 10 inputted values:

Code (Text):

#include iostream
#include string

using namespace std;

int main()
{

vector<double> Values();
double Largest, Smallest, TempVal;
int i;

//Getting the input
while(data.size() < 11)
{
cout << "Input a number and press the enter key: ";
cin >> TempVal;
Values.push_back(TempVal);
};

//Finding The Largest Value
for(i = 0, Largest = Values[0]; i < 10; i++)
{
if(Values[i] > Largest)
{
Largest = Values[i]
};
};

//Finding The Smallest Value
for(i = 0, Smallest = Values[0]; i < 10; i++)
{
if(Values[i] < Smallest)
{
Smallest = Values[i]
};
};

cout << "The largest value is: " << Largest << endl;
cout << "The smallest value is: " << Smallest << endl;

return(0);
};

However I keep getting some random compile errors related to the vector. What am I doing wrong here?

2. Nov 19, 2009

### Hurkyl

Staff Emeritus
It would help if you posted the error -- both to save us time, and so we can help teach you to read them.

But anyways, the first thing I notice is that your #include lines are wrong.

Another thing I notice is that I think the declaration
vector<double> Values();
is bad. You shouldn't have the parentheses there. I believe that makes the compiler think you are trying to prototype a function.

3. Nov 19, 2009

### Lancelot59

Possibly that, but I didn't realize vectors had their own library. :p

Thanks, everything works now. I'll be sure to remove the parentheses, and be on the safe side.

Although when you define the vector wouldn't any compiler be looking for the name immediately after?

4. Nov 19, 2009

### Hurkyl

Staff Emeritus
Oh, I missed the fact you didn't #include <vector>!

There is an ambiguity1 in C++ syntax. How would you declare a function whose name is return type is vector<int> and takes zero arguments? It would look like
vector<int> myfunc();
Note that this is exactly the same form as the line you wrote trying to define a variable of type vector<int> to be constructed with the default constructor, as you did with Values.

1: I lied -- the ambiguity is only in the intuitive sense. I'm pretty sure the C++ standard directs the compiler to interpret it as declaring a function.

5. Nov 19, 2009

### Hurkyl

Staff Emeritus

6. Nov 27, 2009

### Speedo

The problem stems from the fact that you can declare (but not define) one function inside of another.

We're generally used to seeing this:

Code (Text):

void foo();

int main()
{
foo();
return 0;
}

void foo() {}
But this is also just as valid:

Code (Text):
int main()
{
void foo();
foo();
return 0;
}

void foo() {}