There are several problems:
in lines 23-30, you are only ever adding the last point to the vector.
in line 39, you are modifying the point, setting it to an undefined value, I think you meant to swap the positions of the operands.
in line 43, the logic doesn't add up, the first thing that's greater than (still uninitialised) max is called the greatest. You should instead, check everything, replacing max with anything larger that you find. That way, at the end, you'll find that max is the biggest.
Still on line 43, what you have is essentially 2 statements, you have not actually tried to output the value.
You will likely never execute line 43 in any case, if the random value in max is greater than anything entered.
in line 23, by starting from "0" and using "<=", you will actually create 1 extra element in the vector than the user intended, So if they entered a dimension of 5, they end up with 6 points instead.
in line 41, this not how to compare iterators, by "using <=", you end up de-refrencing "end()", which is not a valid part of your container. "end()" points to one location past the end of your container.
Minor annoyances which may just be a question of personal taste include the placement of your using directive, dubious data types (why is max an int?) gratuitous declaration of iterators and surrounding them in parentheses, e.g. (itr)->x, and a style that suggests your C++ implementation is several centuries old.
Have a look at the bare-bones modifications below:
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
|
#include <iostream.h>
#include <conio.h>
#include <vector.h>
#include <math.h>
using namespace std;
struct points {
float x, y;
};
int main()
{
cout << "Enter a dimension of vectors:\n";
int i;
cin >> i;
vector<points>pointvect; //vector declaration
points point;
for (int k = 0; k < i; k++) {
cout << "Input x\n";
cin >> point.x;
cout << "Input y\n";
cin >> point.y;
pointvect.push_back(point); // add point to the vector
}
float max = 0.0;
for (vector<points>::const_iterator itr = pointvect.begin(); itr != pointvect.end(); ++itr){
if (itr->x > max) {
max = itr->x;
}
}
cout << "Maximal X is: " << max << endl;
getch();
return 0;
}
|