Help with expected primary expression

I'm writing a code that calculates radius, area, and perimeter of a circle (the area and perimeter aren't done yet). My issue is that on line 63 when I call the function, it gives me an error "expected primary expression before 'double radius.' When I remove the double radius it says not enough arguments. How can I fix this? I should just be able to call the function correct and the information should be passed? I've worked on similar things involving functions and it's worked just fine. Somebody if you are able to please help I'm quite stumped.

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

using namespace std;

int calculateRadius(double x1, double x2, double y1, double y2)
{
    double radius = sqrt(pow (x2 - x1 , 2) + pow (y2 - y1 , 2));
    cout << "Radius: " << radius << endl;
    return 0;
}

int calculateArea()
{
    return 0;
}

int calculatePerimeter()
{
    return 0;
}

int main() 
{
    double x1,x2,y1,y2;
	cout << "Please enter the center point in the form x y: ";
	cin >> x1 >> y1;
	cout << endl;
	
	while(!cin)
	{
	    cout << "You entered something that is not a number. Please try again" << endl;
	    cin.clear();
	    cin.ignore(INT_MAX, '\n');
	    
	    cout << "Please enter the center point in the form x y: ";
	    cin >> x1 >> y1;
	    cout << endl;
	}
	
	cout << "Please enter the point on the circle in the form x y: ";
	cin >> x2 >> y2;
	cout << endl;
	
		while(!cin)
	{
	    cout << "You entered something that is not a number. Please try again" << endl;
	    cin.clear();
	    cin.ignore(INT_MAX, '\n');
	    
        cout << "Please enter the point on the circle in the form x y: ";
	    cin >> x2 >> y2;
	    cout << endl;
	
	}
	
	cout << "Here is the information for the circle formed from (" << x1 << ","<< y1 
	<< ") and (" << x2 << "," << y2 << ")" << endl;
	
	calculateRadius(double radius);

	return 0;
}
don't put a type on function calls, only when you create them or do headers.
its just
line 63
calculateRadius(radius);
except it isnt, because its
calculateRadius(many, variables, here, ...) //you need all those variables
and, radius isnt created anywhere yet, has to exist before you call a function with it :)

I think you want:
double radius = calculateRadius(x1,x2,y1,y2); //this creates the variable radius and then assigns the result of the function call to it. see how you created this function to accept 4 variables and see how I call it with 4 variables? You can do some smoke and mirrors to call with less than it needs but that is another topic for another day. Generally, you want a one to one parameter called to defined structure.
but you can't create them inside the call, so this is wrong:
double radius = calculateRadius(double x12, double x13, double y42, double y45);

and a little more: radius is a double in the calculate function, but the function returns int. it should return radius and be of type double:

1
2
3
4
5
6
7
8

double calculateRadius(double x1, double x2, double y1, double y2)
{
    double radius = sqrt(pow (x2 - x1 , 2) + pow (y2 - y1 , 2));
    cout << "Radius: " << radius << endl;
    return radius;
}

this allows you to USE the function's result, beyond just printing it to the screen and losing it forever.
the radius in the function and the one in main are NOT the same variable, so you still need to copy it (assign it) into the one in main as I showed above.
Last edited on
Consider:

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

using namespace std;

double calculateRadius(double x1, double y1, double x2, double y2)
{
	return {sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2))};
}

double calculateArea(double rad)
{
	return rad * rad * M_PI;
}

double calculatePerimeter(double rad)
{
	return 2 * rad * M_PI;
}

void getPoints(const string& prm, double& x, double& y)
{
	while ((std::cout << prm) && (!(std::cin >> x >> y) || std::cin.peek() != '\n')) {
		std::cout << "Not two numbers\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

int main()
{
	double x1 {}, x2 {}, y1 {}, y2 {};

	getPoints("Please enter the centre point in the form x y: ", x1, y1);

	getPoints("Please enter the point on the circle in the form x y: ", x2, y2);

	cout << "Here is the information for the circle formed from (" << x1 << "," << y1
		<< ") and (" << x2 << "," << y2 << ")" << endl;

	const auto rad {calculateRadius(x1, y1, x2, y2)};

	cout << fixed << setprecision(2);
	cout << "The radius is: " << rad << '\n';
	cout << "The area is: " << calculateArea(rad) << '\n';
	cout << "The circumference is " << calculatePerimeter(rad) << '\n';
}

Topic archived. No new replies allowed.