Getting multiple values from functions

Hello, I have the following code:

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
#include <iostream>
#include <math.h>
using namespace std;

double get_x(int cardinality_x){

    int the_value;
    cout << "Enter x value number " << cardinality_x << ": ";
    cin >> the_value;
    cout << endl;
    return the_value;
}

double get_y(int cardinality_y){

    int the_value;
    cout << "Enter y value number " << cardinality_y << ": ";
    cin >> the_value;
    cout << endl;
    return the_value;
}

double point_length(double init_x, double init_y, double land_x, double land_y){

    double length = sqrt(((init_x-land_x)*(init_x-land_x))+((init_y-land_y)*(init_y-land_y)));
    return length;
}

int main(void){

    double x1 = get_x(1);
    double y1 = get_y(1);
    double x2 = get_x(2);
    double y2 = get_y(2);
    double x3 = get_x(3);
    double y3 = get_y(3);

    double first_d = point_length(x1,y1,x2,y2);
    double second_d = point_length(x2,y2,x3,y3);
    double third_d = point_length(x3,y3,x1,y1);

    double semiperimeter = (first_d+second_d+third_d)/2;
    double area = sqrt(semiperimeter*(semiperimeter-first_d)*(semiperimeter-second_d)*(semiperimeter-third_d));
    cout << endl << "The area of your triangle is: " << area;

    return 0;
}


The program should find the area of a triangle, and it is syntactically correct. I tried to save a few lines of code by getting values through functions. However, whenever I try to run the program, I can only enter the first value, and then my program asks for the rest of the values all at once, and then it ends. What am I doing wrong?
Last edited on
fix your code tags please
If you want to return both the perimeter and area for example, you can pass perimeter and area by reference to a void function, or use a function that returns a pointer. Do you know pointers?
Nevermind, the problem fixed itself.
Here is what I would change

double x1 = get_x(1);
double y1 = get_y(1);
double x2 = get_x(2);
double y2 = get_y(2);
double x3 = get_x(3);
double y3 = get_y(3);

might I suggest declaring these all to zero, and then giving them values in a for loop inside main, and thereby doing away with your get_y() and get_x() functions.

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 <math.h>
#include <stdio.h>
using namespace std;

double point_length(double init_x, double init_y, double land_x, double land_y){

    double length = sqrt(((init_x-land_x)*(init_x-land_x))+((init_y-land_y)*(init_y-land_y)));
    return length;
}

int main()
{

    double midman(0);
    double x1(0);
    double y1(0);
    double x2(0);
    double y2(0);
    double x3(0);
    double y3(0);

// here is the for loop I added
    for (int i(1); i <= 3; i++) {
    	printf("Enter x value with Cardinality: %i \n", i);
    	cin >> midman;
    	switch(i) {
    		case 1: x1 = midman;
    			break;
    		case 2: x2 = midman;
    			break;
    		case 3: x3 = midman;
    			break;
    	}
    	printf("Enter y value with Cardinality: %i \n", i);
    	cin >> midman;
    	switch(i) {
    		case 1: y1 = midman;
    			break;
    		case 2: y2 = midman;
    			break;
    		case 3: y3 = midman;
    			break;
    	}
    }
// loop ends

    double first_d = point_length(x1,y1,x2,y2);
    double second_d = point_length(x2,y2,x3,y3);
    double third_d = point_length(x3,y3,x1,y1);

    double semiperimeter = (first_d+second_d+third_d)/2;
    double area = sqrt(semiperimeter*(semiperimeter-first_d)*(semiperimeter-second_d)*(semiperimeter-third_d));
    cout << endl << "The area of your triangle is: " << area;

    return 0;
}


... also use camelCase ;)
Last edited on
Topic archived. No new replies allowed.