Vector issues

Feb 28, 2014 at 4:33am
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;

}
Feb 28, 2014 at 4:55am
 
while (cin >> input); 


why you terminate while loop here?

This is the place where you did mistake.

Last edited on Feb 28, 2014 at 4:58am
Feb 28, 2014 at 4:57am
Not sure! I didn't notice that!
Feb 28, 2014 at 4:59am
I took it out and now it gives me an error of expected ;
Feb 28, 2014 at 4:59am

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;
}
Feb 28, 2014 at 5:05am
Whats #include <conio.h> and getch() says it's undeclared
Feb 28, 2014 at 5:22am
Oh remove it, no need of it.
just remove these two lines.
Feb 28, 2014 at 5:32am
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;

}
Feb 28, 2014 at 5:32am
Thank you!
Topic archived. No new replies allowed.