Hi,
I'm trying to get one of my C++ assignments to work but I am stuck.
I get the following errors below and I can't figure out how to fix it.
I also posted the whole program after that, but the line giving me grief is:
dist = sqrt(xtwo - xone, 2) + sqrt(ytwo - yone, 2);
My assignment is due 1 hour from now, so hopefully somebody can help me or I eventually figure it out...

ERRORS:
[
[email protected] ~]$ g++ assignment_seven.cpp
assignment_seven.cpp: In function `double distance(int, int, int, int)':
assignment_seven.cpp:85: no matching function for call to `sqrt(int, int)'
/usr/include/bits/mathcalls.h:146: candidates are: double sqrt(double)
/usr/include/g++-v3/bits/std_cmath.h:461: long double
std::sqrt(long double)
/usr/include/g++-v3/bits/std_cmath.h:455: float
std::sqrt(float)
assignment_seven.cpp:85: no matching function for call to `sqrt(int, int)'
/usr/include/bits/mathcalls.h:146: candidates are: double sqrt(double)
/usr/include/g++-v3/bits/std_cmath.h:461: long double
std::sqrt(long double)
/usr/include/g++-v3/bits/std_cmath.h:455: float
std::sqrt(float)
PROGRAM:
// This program is designed to read a series of data
// sets. Each data set consists of 4 integers representing
// the x and y coordinates of 2 points.
// For each pair of points read, the program will compute and
// print the distance between the points. Compute the slope and
// y-intercept of the line determined by the two points and
// print the equation of the line in standard form (y=mx+b).
// Data will continue to be processed until eof is reached.
#include #include #include using namespace std;
double distance(int,int,int,int);
double find_slope(int,int,int,int);
double get_yintercept(int,int,double);
int main()
{
int x1,y1; // x and y coordinates of point 1
int x2,y2; // x and y coordinates of point 2
double dist; // distance between the 2 points
double slope; // slope of the line defined by the 2 points
double y_intcpt; // place at which the line intercepts the y-axis
cout<<setprecision(3)<<fixed<<showpoint;
cin >> x1;
while (!cin.eof())
{
cin >> y1 >> x2 >> y2;
cout<<"The 2 points are ("<<x1<<","<<y1<<") and ("<<x2<<","<<y2<<")
";
dist = distance(x1,y1,x2,y2);
cout<<"The distance between them is "<<dist<<endl;
if (x1 != x2)
{
slope = find_slope(x1,y1,x2,y2);
y_intcpt = get_yintercept(x1,y1,slope);
if (slope == 0)
{
cout<<"The equation of the line is: y = "<<y_intcpt<<"
";
}
else if (y_intcpt < 0)
{
y_intcpt = -1 * y_intcpt;
cout<<"The equation of the line is: y = "<<slope<<"x - "
<<y_intcpt<<"
";
}
else if (y_intcpt == 0)
{
cout<<"The equation of the line is: y = "<<slope<<"x"<<"
";
}
else
{
cout<<"The equation of the line is: y = "<<slope<<"x + "
<<y_intcpt<<"
";
}
}
else // line with no slope (vertical)
{
cout<<"The equation of the line is: x = "<<x1<<endl<<endl;
}
cin >> x1;
} // end while
return 0;
} // end main
double distance(int xone, int yone, int xtwo, int ytwo)
{
double dist;
dist = sqrt(xtwo - xone, 2) + sqrt(ytwo - yone, 2);
return dist;
}
double find_slope(int x_one, int y_one, int x_two, int y_two)
{
double slope;
slope = (y_two - y_one) / (x_two - x_one);
return slope;
}
double get_yintercept(int xone1, int yone1 , double m)
{
double y_intcpt;
y_intcpt = yone1 - (m * xone1);
return y_intcpt;
}
Comments
You probably mean this:
[code]
dist = sqrt(xtwo - xone) + sqrt(ytwo - yone);
[/code]
: Hi,
: I'm trying to get one of my C++ assignments to work but I am stuck.
: I get the following errors below and I can't figure out how to fix it.
: I also posted the whole program after that, but the line giving me grief is:
:
: dist = sqrt(xtwo - xone, 2) + sqrt(ytwo - yone, 2);
:
: My assignment is due 1 hour from now, so hopefully somebody can help me or I eventually figure it out...
:
: ERRORS:
: [[email protected] ~]$ g++ assignment_seven.cpp
: assignment_seven.cpp: In function `double distance(int, int, int, int)':
: assignment_seven.cpp:85: no matching function for call to `sqrt(int, int)'
: /usr/include/bits/mathcalls.h:146: candidates are: double sqrt(double)
: /usr/include/g++-v3/bits/std_cmath.h:461: long double
: std::sqrt(long double)
: /usr/include/g++-v3/bits/std_cmath.h:455: float
: std::sqrt(float)
: assignment_seven.cpp:85: no matching function for call to `sqrt(int, int)'
: /usr/include/bits/mathcalls.h:146: candidates are: double sqrt(double)
: /usr/include/g++-v3/bits/std_cmath.h:461: long double
: std::sqrt(long double)
: /usr/include/g++-v3/bits/std_cmath.h:455: float
: std::sqrt(float)
:
: PROGRAM:
: // This program is designed to read a series of data
: // sets. Each data set consists of 4 integers representing
: // the x and y coordinates of 2 points.
: // For each pair of points read, the program will compute and
: // print the distance between the points. Compute the slope and
: // y-intercept of the line determined by the two points and
: // print the equation of the line in standard form (y=mx+b).
: // Data will continue to be processed until eof is reached.
:
: #include
: #include
: #include
: using namespace std;
:
: double distance(int,int,int,int);
: double find_slope(int,int,int,int);
: double get_yintercept(int,int,double);
:
: int main()
: {
: int x1,y1; // x and y coordinates of point 1
: int x2,y2; // x and y coordinates of point 2
: double dist; // distance between the 2 points
: double slope; // slope of the line defined by the 2 points
: double y_intcpt; // place at which the line intercepts the y-axis
:
: cout<<setprecision(3)<<fixed<<showpoint;
:
: cin >> x1;
: while (!cin.eof())
: {
: cin >> y1 >> x2 >> y2;
:
: cout<<"The 2 points are ("<<x1<<","<<y1<<") and ("<<x2<<","<<y2<<")
";
:
: dist = distance(x1,y1,x2,y2);
:
: cout<<"The distance between them is "<<dist<<endl;
:
: if (x1 != x2)
: {
: slope = find_slope(x1,y1,x2,y2);
: y_intcpt = get_yintercept(x1,y1,slope);
:
: if (slope == 0)
: {
: cout<<"The equation of the line is: y = "<<y_intcpt<<"
";
: }
: else if (y_intcpt < 0)
: {
: y_intcpt = -1 * y_intcpt;
: cout<<"The equation of the line is: y = "<<slope<<"x - "
: <<y_intcpt<<"
";
: }
: else if (y_intcpt == 0)
: {
: cout<<"The equation of the line is: y = "<<slope<<"x"<<"
";
: }
: else
: {
: cout<<"The equation of the line is: y = "<<slope<<"x + "
: <<y_intcpt<<"
";
: }
:
: }
: else // line with no slope (vertical)
: {
: cout<<"The equation of the line is: x = "<<x1<<endl<<endl;
: }
: cin >> x1;
:
: } // end while
:
: return 0;
:
: } // end main
:
: double distance(int xone, int yone, int xtwo, int ytwo)
: {
: double dist;
: dist = sqrt(xtwo - xone, 2) + sqrt(ytwo - yone, 2);
:
: return dist;
: }
:
: double find_slope(int x_one, int y_one, int x_two, int y_two)
: {
: double slope;
: slope = (y_two - y_one) / (x_two - x_one);
:
: return slope;
: }
:
: double get_yintercept(int xone1, int yone1 , double m)
: {
: double y_intcpt;
: y_intcpt = yone1 - (m * xone1);
:
: return y_intcpt;
: }
:
:
Greets,
Eric Goldstein
www.gvh-maatwerk.nl
: The sqrt-function only takes one argument. You provide two for both calls.
: You probably mean this:
: [code]
: dist = sqrt(xtwo - xone) + sqrt(ytwo - yone);
: [/code]
D'oh I knew that...
BUT I did try what you wrote, and I still can't get the line right, I get the following errors-
[code]
[[email protected] ~]$ g++ assignment_seven.cpp
assignment_seven.cpp: In function `double distance(int, int, int, int)':
assignment_seven.cpp:90: call of overloaded `sqrt(int)' is ambiguous
/usr/include/bits/mathcalls.h:146: candidates are: double sqrt(double)
/usr/include/g++-v3/bits/std_cmath.h:461: long double
std::sqrt(long double)
/usr/include/g++-v3/bits/std_cmath.h:455: float
std::sqrt(float)
assignment_seven.cpp:90: call of overloaded `sqrt(int)' is ambiguous
/usr/include/bits/mathcalls.h:146: candidates are: double sqrt(double)
/usr/include/g++-v3/bits/std_cmath.h:461: long double
std::sqrt(long double)
/usr/include/g++-v3/bits/std_cmath.h:455: float
std::sqrt(float)
[/code]
i see where you used dist = sqrt(xtwo - xone) + sqrt(ytwo - yone);
however i am still looking for the function call in main().
just got out of class myself, and
as a matter of fact still at school.
I did not run this yet just glanced at it.
I think that means the function has not been called
and there may be assignments elsewhere.
-bryce
: The sqrt-function only takes one argument. You provide two for both calls.
: You probably mean this:
: [code]
: dist = sqrt(xtwo - xone) + sqrt(ytwo - yone);
: [/code]
:
: : Hi,
: : I'm trying to get one of my C++ assignments to work but I am stuck.
: : I get the following errors below and I can't figure out how to fix it.
: : I also posted the whole program after that, but the line giving me grief is:
: :
: : dist = sqrt(xtwo - xone, 2) + sqrt(ytwo - yone, 2);
: :
: : My assignment is due 1 hour from now, so hopefully somebody can help me or I eventually figure it out...
: :
: : ERRORS:
: : [[email protected]bby ~]$ g++ assignment_seven.cpp
: : assignment_seven.cpp: In function `double distance(int, int, int, int)':
: : assignment_seven.cpp:85: no matching function for call to `sqrt(int, int)'
: : /usr/include/bits/mathcalls.h:146: candidates are: double sqrt(double)
: : /usr/include/g++-v3/bits/std_cmath.h:461: long double
: : std::sqrt(long double)
: : /usr/include/g++-v3/bits/std_cmath.h:455: float
: : std::sqrt(float)
: : assignment_seven.cpp:85: no matching function for call to `sqrt(int, int)'
: : /usr/include/bits/mathcalls.h:146: candidates are: double sqrt(double)
: : /usr/include/g++-v3/bits/std_cmath.h:461: long double
: : std::sqrt(long double)
: : /usr/include/g++-v3/bits/std_cmath.h:455: float
: : std::sqrt(float)
: :
: : PROGRAM:
: : // This program is designed to read a series of data
: : // sets. Each data set consists of 4 integers representing
: : // the x and y coordinates of 2 points.
: : // For each pair of points read, the program will compute and
: : // print the distance between the points. Compute the slope and
: : // y-intercept of the line determined by the two points and
: : // print the equation of the line in standard form (y=mx+b).
: : // Data will continue to be processed until eof is reached.
: :
: : #include
: : #include
: : #include
: : using namespace std;
: :
: : double distance(int,int,int,int);
: : double find_slope(int,int,int,int);
: : double get_yintercept(int,int,double);
: :
: : int main()
: : {
: : int x1,y1; // x and y coordinates of point 1
: : int x2,y2; // x and y coordinates of point 2
: : double dist; // distance between the 2 points
: : double slope; // slope of the line defined by the 2 points
: : double y_intcpt; // place at which the line intercepts the y-axis
: :
: : cout<<setprecision(3)<<fixed<<showpoint;
: :
: : cin >> x1;
: : while (!cin.eof())
: : {
: : cin >> y1 >> x2 >> y2;
: :
: : cout<<"The 2 points are ("<<x1<<","<<y1<<") and ("<<x2<<","<<y2<<")
";
: :
: : dist = distance(x1,y1,x2,y2);
: :
: : cout<<"The distance between them is "<<dist<<endl;
: :
: : if (x1 != x2)
: : {
: : slope = find_slope(x1,y1,x2,y2);
: : y_intcpt = get_yintercept(x1,y1,slope);
: :
: : if (slope == 0)
: : {
: : cout<<"The equation of the line is: y = "<<y_intcpt<<"
";
: : }
: : else if (y_intcpt < 0)
: : {
: : y_intcpt = -1 * y_intcpt;
: : cout<<"The equation of the line is: y = "<<slope<<"x - "
: : <<y_intcpt<<"
";
: : }
: : else if (y_intcpt == 0)
: : {
: : cout<<"The equation of the line is: y = "<<slope<<"x"<<"
";
: : }
: : else
: : {
: : cout<<"The equation of the line is: y = "<<slope<<"x + "
: : <<y_intcpt<<"
";
: : }
: :
: : }
: : else // line with no slope (vertical)
: : {
: : cout<<"The equation of the line is: x = "<<x1<<endl<<endl;
: : }
: : cin >> x1;
: :
: : } // end while
: :
: : return 0;
: :
: : } // end main
: :
: : double distance(int xone, int yone, int xtwo, int ytwo)
: : {
: : double dist;
: : dist = sqrt(xtwo - xone, 2) + sqrt(ytwo - yone, 2);
: :
: : return dist;
: : }
: :
: : double find_slope(int x_one, int y_one, int x_two, int y_two)
: : {
: : double slope;
: : slope = (y_two - y_one) / (x_two - x_one);
: :
: : return slope;
: : }
: :
: : double get_yintercept(int xone1, int yone1 , double m)
: : {
: : double y_intcpt;
: : y_intcpt = yone1 - (m * xone1);
: :
: : return y_intcpt;
: : }
: :
: :
:
:
: Greets,
: Eric Goldstein
: www.gvh-maatwerk.nl
:
:
: : The sqrt-function only takes one argument. You provide two for both calls.
: : You probably mean this:
: : [code]
: : dist = sqrt(xtwo - xone) + sqrt(ytwo - yone);
: : [/code]
:
: D'oh I knew that...
: BUT I did try what you wrote, and I still can't get the line right, I get the following errors-
[blue]
Just read the error msg carefully: it says the sqrt functions expects a double for its argument.
You are passing an int.
If you cast the argument to a double, your problem should be solved:
[code]
dist = sqrt((double)(xtwo - xone)) + sqrt((double)(ytwo - yone));
[/code]
[/blue]
:
: [code]
: [[email protected] ~]$ g++ assignment_seven.cpp
: assignment_seven.cpp: In function `double distance(int, int, int, int)':
: assignment_seven.cpp:90: call of overloaded `sqrt(int)' is ambiguous
: /usr/include/bits/mathcalls.h:146: candidates are: double sqrt(double)
: /usr/include/g++-v3/bits/std_cmath.h:461: long double
: std::sqrt(long double)
: /usr/include/g++-v3/bits/std_cmath.h:455: float
: std::sqrt(float)
: assignment_seven.cpp:90: call of overloaded `sqrt(int)' is ambiguous
: /usr/include/bits/mathcalls.h:146: candidates are: double sqrt(double)
: /usr/include/g++-v3/bits/std_cmath.h:461: long double
: std::sqrt(long double)
: /usr/include/g++-v3/bits/std_cmath.h:455: float
: std::sqrt(float)
: [/code]
:
:
:
Greets,
Eric Goldstein
www.gvh-maatwerk.nl
: : The sqrt-function only takes one argument. You provide two for both calls.
: : You probably mean this:
: : [code]
: : dist = sqrt(xtwo - xone) + sqrt(ytwo - yone);
: : [/code]
:
: D'oh I knew that...
: BUT I did try what you wrote, and I still can't get the line right, I get the following errors-
[blue]
Just read the error msg carefully: it says the sqrt functions expects a double for its argument.
You are passing an int.
If you cast the argument to a double, your problem should be solved:
[code]
dist = sqrt((double)(xtwo - xone)) + sqrt((double)(ytwo - yone));
[/code]
[/blue]
:
: [code]
: [[email protected] ~]$ g++ assignment_seven.cpp
: assignment_seven.cpp: In function `double distance(int, int, int, int)':
: assignment_seven.cpp:90: call of overloaded `sqrt(int)' is ambiguous
: /usr/include/bits/mathcalls.h:146: candidates are: double sqrt(double)
: /usr/include/g++-v3/bits/std_cmath.h:461: long double
: std::sqrt(long double)
: /usr/include/g++-v3/bits/std_cmath.h:455: float
: std::sqrt(float)
: assignment_seven.cpp:90: call of overloaded `sqrt(int)' is ambiguous
: /usr/include/bits/mathcalls.h:146: candidates are: double sqrt(double)
: /usr/include/g++-v3/bits/std_cmath.h:461: long double
: std::sqrt(long double)
: /usr/include/g++-v3/bits/std_cmath.h:455: float
: std::sqrt(float)
: [/code]
:
:
:
Greets,
Eric Goldstein
www.gvh-maatwerk.nl
Try to do the stages separately:
xsqr = (xtwo - xone)*(xtwo - xone);
ysqr = (ytwo - yone)*(ytwo - yone);
dist = sqrt(xsqr + ysqr);
Hope this helps,
: Hi,
: I'm trying to get one of my C++ assignments to work but I am stuck.
: I get the following errors below and I can't figure out how to fix it.
: I also posted the whole program after that, but the line giving me grief is:
:
: dist = sqrt(xtwo - xone, 2) + sqrt(ytwo - yone, 2);
:
: My assignment is due 1 hour from now, so hopefully somebody can help me or I eventually figure it out...
:
: ERRORS:
: [[email protected] ~]$ g++ assignment_seven.cpp
: assignment_seven.cpp: In function `double distance(int, int, int, int)':
: assignment_seven.cpp:85: no matching function for call to `sqrt(int, int)'
: /usr/include/bits/mathcalls.h:146: candidates are: double sqrt(double)
: /usr/include/g++-v3/bits/std_cmath.h:461: long double
: std::sqrt(long double)
: /usr/include/g++-v3/bits/std_cmath.h:455: float
: std::sqrt(float)
: assignment_seven.cpp:85: no matching function for call to `sqrt(int, int)'
: /usr/include/bits/mathcalls.h:146: candidates are: double sqrt(double)
: /usr/include/g++-v3/bits/std_cmath.h:461: long double
: std::sqrt(long double)
: /usr/include/g++-v3/bits/std_cmath.h:455: float
: std::sqrt(float)
:
: PROGRAM:
: // This program is designed to read a series of data
: // sets. Each data set consists of 4 integers representing
: // the x and y coordinates of 2 points.
: // For each pair of points read, the program will compute and
: // print the distance between the points. Compute the slope and
: // y-intercept of the line determined by the two points and
: // print the equation of the line in standard form (y=mx+b).
: // Data will continue to be processed until eof is reached.
:
: #include
: #include
: #include
: using namespace std;
:
: double distance(int,int,int,int);
: double find_slope(int,int,int,int);
: double get_yintercept(int,int,double);
:
: int main()
: {
: int x1,y1; // x and y coordinates of point 1
: int x2,y2; // x and y coordinates of point 2
: double dist; // distance between the 2 points
: double slope; // slope of the line defined by the 2 points
: double y_intcpt; // place at which the line intercepts the y-axis
:
: cout<<setprecision(3)<<fixed<<showpoint;
:
: cin >> x1;
: while (!cin.eof())
: {
: cin >> y1 >> x2 >> y2;
:
: cout<<"The 2 points are ("<<x1<<","<<y1<<") and ("<<x2<<","<<y2<<")
";
:
: dist = distance(x1,y1,x2,y2);
:
: cout<<"The distance between them is "<<dist<<endl;
:
: if (x1 != x2)
: {
: slope = find_slope(x1,y1,x2,y2);
: y_intcpt = get_yintercept(x1,y1,slope);
:
: if (slope == 0)
: {
: cout<<"The equation of the line is: y = "<<y_intcpt<<"
";
: }
: else if (y_intcpt < 0)
: {
: y_intcpt = -1 * y_intcpt;
: cout<<"The equation of the line is: y = "<<slope<<"x - "
: <<y_intcpt<<"
";
: }
: else if (y_intcpt == 0)
: {
: cout<<"The equation of the line is: y = "<<slope<<"x"<<"
";
: }
: else
: {
: cout<<"The equation of the line is: y = "<<slope<<"x + "
: <<y_intcpt<<"
";
: }
:
: }
: else // line with no slope (vertical)
: {
: cout<<"The equation of the line is: x = "<<x1<<endl<<endl;
: }
: cin >> x1;
:
: } // end while
:
: return 0;
:
: } // end main
:
: double distance(int xone, int yone, int xtwo, int ytwo)
: {
: double dist;
: dist = sqrt(xtwo - xone, 2) + sqrt(ytwo - yone, 2);
:
: return dist;
: }
:
: double find_slope(int x_one, int y_one, int x_two, int y_two)
: {
: double slope;
: slope = (y_two - y_one) / (x_two - x_one);
:
: return slope;
: }
:
: double get_yintercept(int xone1, int yone1 , double m)
: {
: double y_intcpt;
: y_intcpt = yone1 - (m * xone1);
:
: return y_intcpt;
: }
:
:
:
: Try to do the stages separately:
:
: xsqr = (xtwo - xone)*(xtwo - xone);
:
: ysqr = (ytwo - yone)*(ytwo - yone);
:
: dist = sqrt(xsqr + ysqr);
:
: Hope this helps,
:
yes that worked! thank you!