Vector finding max value errors?

trying to find the max number in a vector were you input the numbers.

ERRORS:
max1.cpp:39: error: invalid operands of types `int' and `<unresolved
overloaded function type>' to binary `operator>'
max1.cpp:41: error: overloaded function with no contextual type
information
max1.cpp:44: error: cannot resolve overloaded function `max' based
on conversion to type `int'

Thank you for your help in advance i really appreciate it.



#include <iostream>
#include <vector>
using namespace std;

void print(vector<int> a);
int max(vector<int> b);

int main ()
{
vector<int> v;
int number, x;
cout <<"Input numbers (-999 to stop)\n";
do
{
cin>>number;
v.push_back(number);
}
while (number !=-999);
v.pop_back();
print(v);
x = max(v);
cout <<"The max number: "<<x<<endl;
return 0;
}

void print (vector<int> a)
{ cout <<"The list is :"<<endl;
for(int i=0; i < (int)a.size(); i++)
{
cout << a[i] << " ";
}
cout << endl;
}

//problems in the max function, don't know if i wrote it right.

int max(vector<int> b)
{
for (int i=0; i < (int)b.size(); i++)
{
if (b[i] > max )
{
max = b[i];
}
}
return max;
}
Last edited on
Seriously, is it that hard to explain your problem in plain english, starting with "Hello" and ending with "Thank you for your help" ?

You're lucky I saw the problem right away, otherwise I wouldn't even have tried.

Your problems:
- Your function, named max, uses a variable named max. You need to change one of them.
- The variable used to compute the max is not initialized. Initialize it to b[0]
When the compiler will be parse statement

max = b[i];

it shall issue an error because you can not assign a value to a function pointer.
Last edited on
First thanks for the help.I understand the first part about the variables having the same name.
-the second one i don't understand were i should intialize b[0] because more errors pop up if i do it before the for loop?
Sorry if I'm being difficult.
Last edited on
Mau,

In all frankness, I'd suggest you put aside your IDE for a moment or two and focus on the layout of your program.

Begin, say in NotePad or VI with the requirements


"trying to find the max number in a vector were you input the numbers."


#1) (get) input the numbers
#2) (place) into a vector
#3) find the max number
#4) Display results
#5) loop or exit

MAIN()
{
get_num()
add_vec()
find_max)
show_me()
more_loop()
}

think carefully about what variables you will need,
and assign them a descriptive name, one that is clear as to what type it is and what its purpose is.

int a_num_store[666]
int i_array_pntr
int i_injput_val
double d_temp_calc
float f_max_val

Do not use var names like i,j,n,m,x,y as they are misleading, confusing, and easily incorrect.

Layout your subrtn's and comment them heavily as to what they are suppose to do and what your ARG's are suppose to do, and what your RTN values are suppose to be. If your return value is suppose to be an a String, then say so in a comment at the beginning of the subrt'n.

Once you have this written up in notepad or VI or your favorite editor, THEN you can load it into your IDE and begin to wrap some C++ code around it.



thank you all specially toum since that actually worked execpt i had to initialize max=o not b[0].
Topic archived. No new replies allowed.