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;
}
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]
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.
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.