input xy coordinates and stuff

closed account (SN0i1hU5)
What is wrong with my program?

I'm trying to write a program that finds and returns the midpoint of two xy coordinates and ask the user if they wish to continue or not. I don't know how i would go about inputting the coordinates. I never used struct Point for anything before.


Here's my code 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
#include <iostream>
#include <string>
#include <cctype>
#include <sstream>
using namespace std;

struct Point{
	int x;
	int y;
	int x2;
	int y2;
}name;

Point midpoint(const Point& a, const Point& b)
{
	Point vec;
	vec.x = (a.x + b.x) / 2;
	vec.y = (a.y + b.y) / 2;
	return vec;
}

int main()
{
	string mystr;
	int n;
	for (n = 0; n < 100; n++)
	{
		cout << "Enter 1st Point(x,y): ";
		getline (cin, name.x, name.y); //I seem to be getting an error here.
		cout << "Enter 2nd Point(x,y): ";
		getline (cin, name.x2, name.y2);
		
		cout << "midpoint between" << name.x << ", " << name.y << " and " << name.x2 << ", " << name.y2 << " is " << vec <<endl;
		
		cout << "Do you want to continue?: ";
		cin >> choice;
		char choice;
		if (choice == 'y')
		{
			cout << "Enter 1st Point(x,y): ";
			getline (cin, name.x, name.y);
			cout << "Enter 2nd Point(x,y): ";
			getline (cin, name.x, name.y);
		
			cout << "Do you want to continue?: ";
			cin >> choice;
		}
		else if (choice == 'n')
		{
			break;
		}
	}
	
}
Line 10-11: Why does Point have two x and two y values? A point should have a single x and a single y value.

Line 17: Why is name global? Globals should be avoided.

Line 29,31: That's not the correct syntax for getline. getline expects a string. Use cin instead.
1
2
3
4
  Point p1, p2;
  cin >> p1.x >> p1.y;
...
  cin >> p2.x >> p2.y;


Line 33: You're trying to output vec. Several problems here.
1) You haven't provided an overload of the << operator, so C++ doesn't know how to format a Point.
2) You haven't defined vec, nor called midpoint() to set its values.
3) Your midpoint() function uses b.x and b.y for the second point, but you input the second pair of values into the name.x2 and name.y2.

Line 38-47: These lines are unnecessary. Just let the loop return to the top.

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
#include <iostream>
#include <string>
using namespace std;

struct Point
{   int x;
	int y;
	//  Overload the << operator
	friend ostream & operator << (ostream & os, const Point p)
	{   os << p.x << "," << p.y;
	    return os;
	}
};

Point midpoint (const Point& a, const Point& b)
{   Point vec;
	vec.x = (a.x + b.x) / 2;
	vec.y = (a.y + b.y) / 2;
	return vec;
}

int main()
{   Point p1, p2;
    Point vec;
	int n;
	char choice;
	
	for (n = 0; n < 100; n++)
	{
		cout << "Enter 1st Point(x,y): ";
		cin >> p1.x >> p1.y;    
		cout << "Enter 2nd Point(x,y): ";
		cin >> p2.x >> p2.y; 
	    vec = midpoint (p1, p2);	
		cout << "midpoint between" << p1 << " and " << p2 << " is " << vec << endl;		
		cout << "Do you want to continue?: ";
		cin >> choice;
        if (choice == 'n')
		    break;		
	}	
}

Last edited on
Topic archived. No new replies allowed.