Need help with structures and vectors

// Your program implements some functionality extensively used in graphical user interfaces (GUI) and computer games. The program collects points/coordinates, given by user and stores them in a container.

// Points are represented by the structure Point
// struct Point
// {
// int x;
// int y;
// }

// The program displays a menu with the following items
// Add a new point
// Select points
// Print all points
// Print selected points
// Exit

// The user enters a number to select an item from the menu. Depending on the user’s choice the program does the following:

// Asks the user to enter x-coordinate and y-coordinate of a new point. Adds this new point to the vector of points.
// Asks the user to enter 2 points. These points define a rectangle. Select all the points inside that rectangle. Selecting is done by storing in a separate vector the indexes of selected points in the main container. It is up to you if you want to select the points located right on the borders of the rectangle. Keep in mind that the user can enter any two points to define a rectangle in any order.
// Prints all points on one line in the following format:

// (12,2) (3,4) (-5,0) (39,101)

// Prints only the selected points in the same format as 3.
// Ends the program execution.


// You must use functions extensively. The function main should do only the minimal work like getting user input. All menu options except Exit should be implemented by a separate function. If a function takes point as a parameter, it should take Point, not separate x- and y-coordinates.

// You should NOT use any global variables. Use references to pass vectors to functions. If the function doesn’t change the vector, it should receive that vector as a const reference

Here's what I have so far:
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

#include <iostream>
#include <vector>

using namespace std;

short menu_buttons (short menu_num);
short add_point();
short select_points();
short print_all_points();
short print_selected_points();

//Main Menu
int main() 
{
cout << "Main fucking Menu\n" 
<< "  1. Add Point to list\n"
<< "  2. Select Points\n"
<< "  3. Show all Points\n"
<< "  4. Show selected points\n"
<< "  5. Exit because you're done ruining my July 4.\n"
<< " Enter a number from the menu:";
short menu_num;
cin >> menu_num;
menu_buttons(menu_num);
return 0;
}

//Menu buttons
short menu_buttons(short menu_num)
{
if (menu_num == 1)
{
  add_point();
}
else if (menu_num == 2)
{
  select_points();
}
else if (menu_num == 3)
{
  print_all_points();
}
else if (menu_num == 4)
{
  print_selected_points();
}
else if (menu_num == 5)
{
  cout << "\nGood-bye! \n";
  return 0;
}
else if (menu_num != 1)
{
cout << "\nYou've typed bullshit. Restarting menu:\n";
main();
}
return 0;
}

 
short add_point()
{
  cout << "test";
  return 0;
}


short select_points()
{
  cout << "test";
  return 0;
}

short print_all_points()
{
  cout << "test";
  return 0;
}

short print_selected_points()
{
  cout << "test";
  return 0;
}
Last edited on
you need to add the point type as shown in the assignment before you can do much more. Then you can start adding real code to your stub functions to make them do things.

start small. I know it said to do everything in functions, but start by hacking around in main and you can move the code out once you understand it.
-- add the point struct to your code.
-- create a vector of it.
-- assign one location in the vector manually, eg vec[0].x = 2; vec[0].y = 3;

get that working, print out something that proves it worked, and move it into functions from there.
Thanks. I'm actually still learning the syntax of structure and vectors. I don't know how to add coordinators from a structure into the vector.

I also don't know how to print structures out. My professor didn't teach us this but expects us to google everything and figure it out. It's due tonight at 12 PM :(

I have no clue on how to do the "select points" part either.

If you would show me how, I would greatly appreciate it.

I've updated my code a bit and it's in: https://repl.it/@clonxy/HilariousPessimisticTechnologies
Last edited on
I (and many others) can't follow links.

Ill help get you started with a very simple example.

struct Point //this creates a user defined type, akin to type int
{
int x;
int y;
}; // the ; is important, but your assignment missed that detail.

typedef vector<Point> Ptvec; //also creates a type, this saves typing later.

int main()
{
Ptvec p; //same as typing vector<Point> p;
Point tmp;
tmp.x = 2;
tmp.y = 3;
p.push_back(tmp);
}

void add_point(Ptvec &pv, int x, int y)
{ //this seems like something you would want?
Point pt;
pt.x = x; pt.y = y;
pv.push_back(pt);
}

I guess its not obvious, but printing a point looks like

cout << pt.x << " " << pt.y; //you can add text around this as needed.
or from the vector in main
cout << p[index].x << " " << p[index].y;


you can actually tie the functions to the struct. I did not do that because its probably getting 2 chapters ahead of your studies. It is a lot easier once you learn that...

Last edited on
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*David Chen
Home Project 4
*/
#include <iostream>
#include <vector>

using namespace std;

short menu_buttons (short menu_num);
short print_all_points();
short add_point();
short select_points();
short print_selected_points();

struct points
{
  int x,y;
};

//Main Menu
int main() 
{
cout << "Main fucking Menu\n" 
<< "  1. Add Point to list\n"
<< "  2. Select Points\n"
<< "  3. Show all Points\n"
<< "  4. Show selected points\n"
<< "  5. Exit because you're done ruining my July 4.\n"
<< " Enter a number from the menu:"; //space before Enter is done on purpose.
short menu_num;
cin >> menu_num;
menu_buttons(menu_num);
return 0;
}

//Menu buttons
short menu_buttons(short menu_num)
{
if (menu_num == 1)
{
  add_point();
}
else if (menu_num == 2)
{
  select_points();
}
else if (menu_num == 3)
{
  print_all_points();
}
else if (menu_num == 4)
{
  print_selected_points();
}
else if (menu_num == 5)
{
  cout << "\nProgram terminated.\n" << "Good-bye! \n";
  return 0;
}
else if (menu_num != 1)
{
cout << "\nYou've typed bullshit. Restarting menu:\n";
main();
}
return 0;
}
 
short add_point()
{
  vector <int> VectorPoints;
  points point;
  cout << "Enter X-coordinate:\n";
  cin >> point.x;
  cout << "Enter Y-coordinate:\n";
  cin >> point.y;
  cout << "\n\nGoing back to main menu\n\n";
  VectorPoints.push_back(point)
  main();
  return 0;
}


short select_points()
{
  cout << "I don't know how to do this one. This is too hard.\n";
  main();
  return 0;
}

short print_all_points()
{
  short i;
  for (auto i:VectorPoints)
  {
    cout << i << " ";
  }
  main();
  return 0;
}

short print_selected_points()
{
  cout << "I don't know how to do this one. This is too hard.\n";
  main();
  return 0;
}
Last edited on
Nevermind, failed the project.
Topic archived. No new replies allowed.