Better way to input (x, y) with cin or getline(cin, var)?

May 24, 2020 at 2:05am
Hi, one of the questions on my homework asked me to write a program that determines the perimeter length of a triangle given 3 (x, y) vertices. Didn't have an issue writing it, but I know there has to be a better, less clunky way to input the point variables on the same line, possibly separated by a comma. Again, just to clarify, I'm not looking for a solution to my homework problem, I've completed it satisfactorily, I was just looking for a more streamlined way to collect multiple variables on the same line in the future.

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
  #include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

struct Point 
{
    int x, y;
};

struct Triangle
{
    Point a;
    Point b;
    Point c;
};

double perimeter(Triangle uT)
{
    //declare variables
    double dist1, dist2, dist3, result;
    //do maths
    dist1 = sqrt((pow((uT.b.x - uT.a.x), 2)) + (pow((uT.b.y - uT.a.y), 2)));
    dist2 = sqrt((pow((uT.c.x - uT.b.x), 2)) + (pow((uT.c.y - uT.b.y), 2)));
    dist3 = sqrt((pow((uT.c.x - uT.a.x), 2)) + (pow((uT.c.y - uT.a.y), 2)));
    result = dist1 + dist2 + dist3;
    return result;
}

int main()
{
    //declare triangle structure
    Triangle dak;
    //collect points
    cout << "Enter triangle coordinates\n";
    cout << "Point A: ";
    cin >> dak.a.x;
    cin >> dak.a.y;
    cout << "Point B: ";
    cin >> dak.b.x;
    cin >> dak.b.y;
    cout << "Point C: ";
    cin >> dak.c.x;
    cin >> dak.c.y;
    
    //format output
    cout << "\nTriangle with points\n(" << dak.a.x << "," << dak.a.y << "), (";
    cout << dak.b.x << "," << dak.b.y << "), and (" << dak.c.x << "," << dak.c.y << ")\n";
    cout << "has a perimeter of ";
    //collect answer in double
    double answer = perimeter(dak);
    //print result, rounded to 3 decimal places
    cout << fixed << setprecision(3) << answer;

    return 0;
}


As you can see, right now my program collects the x and y values for the vertex points on separate lines. It would be neat to be able to say "Enter x and y values for vertex, separated by a comma" or cout a "( , )" type structure to have the user input the x and y values into.
May 24, 2020 at 2:26am
Alternatively, you could just input all the x,y variables in a single line, like so

1
2
3
    cout << "Enter triangle coordinates for A, B, and C\n";
    cin >> dak.a.x >> dak.a.y >> dak.b.x >> dak.b.y >> dak.c.x >> dak.c.y;


The user can them list all the x and y values separated by spaces:

1 2 7 5 4 4


Translates to (1,2); (7,5); (4,4).

Any more complex string processing would probably require std::regex
May 24, 2020 at 2:45am
Eh, I ended up just keeping it simple. Prof. would probably like it that way seeing as she grades like 60 assignments a week minimum.

1
2
3
4
5
6
7
8
//collect points
    cout << "Enter x and y coordinates separated by a space:\n";
    cout << "Point A: ";
    cin >> dak.a.x >> dak.a.y;
    cout << "Point B: ";
    cin >> dak.b.x >> dak.b.y;
    cout << "Point C: ";
    cin >> dak.c.x >> dak.c.y;
May 24, 2020 at 5:15am
Functions are what you need
1
2
3
4
5
6
7
8
9
10
11
12
void inputPoint( Point &p ) {
  cin >> p.x >> p.y;
}

void inputTriangle( Triangle &t ) {
  cout << "Point A: ";
  inputPoint(t.a);
  cout << "Point B: ";
  inputPoint(t.b);
  cout << "Point C: ";
  inputPoint(t.c);
}


So this.
1
2
3
4
5
6
7
8
9
10
11
12
13
    //declare triangle structure
    Triangle dak;
    //collect points
    cout << "Enter triangle coordinates\n";
    cout << "Point A: ";
    cin >> dak.a.x;
    cin >> dak.a.y;
    cout << "Point B: ";
    cin >> dak.b.x;
    cin >> dak.b.y;
    cout << "Point C: ";
    cin >> dak.c.x;
    cin >> dak.c.y;

becomes
1
2
3
4
5
    //declare triangle structure
    Triangle dak;
    //collect points
    cout << "Enter triangle coordinates\n";
    inputTriangle(dak);

Topic archived. No new replies allowed.