Hello,
I have an assignment to convert some given jave code into c++. I am currently having an issue wherein when I run the program it tells me that: error C2660: 'approximateSqrt' : function does not take 1 arguments. Any suggestions for moving forward would be appreciated. I have the following:

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>

using namespace std;

// Function Prototyping
double getValue();
double getInitialGuess();
double approximateSqrt();



int main()
{
    double x = getValue();
    double xSqrt = approximateSqrt(x);
    cout<<"\nsqrt( %1.6f ) = %1.6f\n", x, xSqrt ;
}

// FUNCTION 1
double getValue()
{
    double value;
    cout<<"Enter value (>0) whose square root will be computed: ";
    cin>>value;
    while ( value <= 0 )
    {

        cout<<"Invalid value "<<value<<" <= 0" ;
        cout<<endl<<" -- try again";
        cout<<endl<<"Enter value (>0): ";
        cin>>value;
    }
    return value;
}

// FUNCTION 2
double getInitialGuess( double value )
{
    return value / 2.0;
}

// FUNCTION 3
double approximateSqrt( double value )
{
    double epsilon = 0.00001;
    double x = getInitialGuess( value );

    //System.out.printf( "sqrt( %1.6f ) = %1.6f\n", value, x );
    cout<<"sqrt( %1.6f ) = %1.6f\n", value, x;
    // SOMETHING TO DO WITH CMATH
    while ( abs( value - x * x ) > epsilon )
    {
        double tx = x;
        x = 0.5 * ( x + ( value / x ) );
        //System.out.printf( "sqrt( %1.6f ) = 0.5 * ( %1.6f + ( %1.6f / %1.6f ) ) = %1.6f\n", value, tx, value, tx, x );
        cout<<"sqrt( %1.6f ) = 0.5 * ( %1.6f + ( %1.6f / %1.6f ) ) = %1.6f\n", value, tx, value, tx, x;
    }
    return x;
}

Dani AI

Generated

Good catch by — the C2660 error is a classic declaration/definition mismatch. The compiler treats the prototype at the top of the file as the function signature it expects; if the prototype says “no parameters” but you call the function with one, the call doesn’t match any declared overload and you get C2660. Make sure every prototype exactly matches the function definition (types and parameter count). After that fix, the algorithm itself looks fine.

Two quick portability/practical notes when translating Java-style prints and numerics to C++:

  • Don’t try to use Java/printf format specifiers inside a C++ stream insertion expression. Either use C-style formatting:

    std::printf("sqrt(%1.6f) = %1.6f\n", value, result);

    or use <iomanip> with streams:

    std::cout << "sqrt(" << std::fixed << std::setprecision(6)
            << value << ") = " << result << '\n';
  • For the convergence test use the floating-point absolute function appropriate to C++: std::abs (or std::fabs) from <cmath> so the comparison with epsilon works on double. Also guard against dividing by zero if you ever allow zero as input (your getValue currently enforces >0, which avoids that).

Quick checklist for similar problems:

  • Compare every prototype and definition; parameter lists must match exactly.
  • Enable compiler warnings (/W4 on MSVC, -Wall -Wextra on GCC/Clang).
  • Fix any printf-style strings used with cout.
  • Prefer explicit std:: names in examples to avoid namespace confusion in larger projects.

Nice follow-up, — you were on the right track and pointed out the key fix.

Recommended Answers

All 2 Replies

On line 11 your function prototype says that approximateSqrt() takes no parameters so that is where that error is coming from. To fix it change that line to double approximateSqrt(double);

Thanks for helping me even on Easter. I finished my code and that was what I needed to fix. Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.