Vector issues

Hi so the goal of this program is to find the largest element calling from maxv but for some reason it keeps giving me the last value. I'd appreciate any help, thanks!

int maxv (vector<int>v)

{
int max = v[0];

for(int i = 0; i < v.size(); ++i){

if (max < v[i])

max = v[i];

}
return max;



}


int main()
{
int input;
vector <int> v;


//Input values
cout << "Input numbers:\n";

//convert string to vector
while (cin >> input);
{
v.push_back(input);
}
//Read out max value
cout << "\nMaximum value was v["<<v.size() "] = "<< maxv (v);


return 0;

}
 
while (cin >> input); 


why you terminate while loop here?

This is the place where you did mistake.

Last edited on
Not sure! I didn't notice that!
I took it out and now it gives me an error of expected ;

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#include <string>
#include <conio.h>
#include <iostream>
#include <vector>

using namespace std;


int maxv (vector<int>v)

{
int max = v[0];

for(int i = 0; i < v.size(); ++i)
{
	if (max < v[i])
	max = v[i];
}
return max;

}


int main()
{
int input;
vector <int> v;
int i=0;
//Input values
cout << "Input numbers:\n";

//convert string to vector
while (i<5)
{
	cin >> input;
v.push_back(input);
i++;
}
//Read out max value
cout << "\nMaximum value was v"<<endl;
cout<< maxv(v);
getch();
return 0;
}
Whats #include <conio.h> and getch() says it's undeclared
Oh remove it, no need of it.
just remove these two lines.
Please use code tags
http://www.cplusplus.com/articles/z13hAqkS/

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
29
30
31
32
33
34
35
36
#include <iostream>
#include <vector>
#include <sstream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
	string temp = "";
	vector<double> numVec;

	//get a line from user
	cout << "Please enter a list of numbers: " << flush;
	getline(cin, temp);
	

	stringstream ss(temp);
	double tempDouble = 0;
	
	//convert userinput to double
	while (ss >> tempDouble)
	{
		numVec.push_back(tempDouble);
	}

	//returns an iterater to the max number
	auto max = max_element(numVec.begin(), numVec.end());

	cout << "The maximum number is " << *max << endl;

	cin.ignore();
	return 0;

}
Thank you!
Topic archived. No new replies allowed.