Thread: sqrt function

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    30

    sqrt function

    this is another simple (i think) function that i have to complete for an assignment. the assignment description is:
    Write a program with a function called mySquareRoot. In main, prompt the user for a number (a double), then – by calling the mySquareRoot function – calculate and display the square root of the number. Use the standard cmath function to calculate the square root. Stop processing numbers when the user enters a zero.
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int num;
    
    double mySquareRoot(int num)
    {
        return sqrt(num);
    }
    
    int main()
    {
       
        do
        {
            cout << "Enter a number (a double): ";
            cin >> num;
            cout << "The square root is " << mySquareRoot;
        } while (num != 0);
    }
    i am pretty sure my main program is right (with the do..while loop), but my function obviously isnt working correctly. it wont compile correctly, so i dont even know if i am getting close.

    thanks for help
    Last edited by kbpsu; 04-01-2009 at 02:43 PM.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    40
    mySquareRoot(num)

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    do i put that instead of the 'double mysquareroot(int num)'?

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    14
    You're not calling mySquareRoot() correctly in the do loop. In that loop, replace mySquareRoot with mySquareRoot(num).

    The way your loop is structured right now, you'll try to find the square root of 0 when that is what the user enters. You may want to exit immediately when 0 is entered, instead of exiting after finding its square root.

    I'd also recommend moving the definition of int num into main(). There's no need for it to be a global.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    do i put that instead of the 'double mysquareroot(int num)'?
    No - your function prototype defines what your function returns and accepts as input, which is correct. What you need to do is actually supply that input. Hence the advice given by DirkMaas...

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    ive gotten rid of a lot of errors now with your help, this is what i have so far.
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    double mySquareRoot()
    {
        return sqrt(num);
    }
    
    int main()
    {
    	int num;
       
        do
        {
            cout << "Enter a number (a double): ";
            cin >> num;
            cout << "The square root is " << mySquareRoot(num);
        } while (num != 0);
    }
    the only errors i am getting now are:
    Code:
    1>c:\documents and settings\bruce\my documents\visual studio 2008\projects\ass25\ass25\ass25.cpp(7) : error C2065: 'num' : undeclared identifier
    1>c:\documents and settings\bruce\my documents\visual studio 2008\projects\ass25\ass25\ass25.cpp(18) : error C2660: 'mySquareRoot' : function does not take 1 arguments
    is this telling me that i do need to declare num before the main function?

  7. #7
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89
    int num as it stands is local to the main() function so yes, it needs to go before the functions to make it global (available to the entire program).

    The other error is that the mySquareRoot function does not have the argument type declared:

    Quote Originally Posted by kbpsu View Post
    the only errors i am getting now are:
    Code:
    error C2065: 'num' : undeclared identifier
    error C2660: 'mySquareRoot' : function does not take 1 arguments
    is this telling me that i do need to declare num before the main function?

    ive gotten rid of a lot of errors now with your help, this is what i have so far.
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    **int num;**
    
    double mySquareRoot(**int**)
    {
        return sqrt(num);
    }
    
    int main()
    {
    	--int num;--
       
        do
        {
            cout << "Enter a number (a double): ";
            cin >> num;
            cout << "The square root is " << mySquareRoot(num) **<< endl**;
        } while (num != 0);
    }
    **additions**
    --remove--
    Last edited by BuzzBuzz; 04-01-2009 at 04:41 PM.
    Any help I give may be classified as:
    The Blind leading the Blind...
    Currently working through:
    "C++ Primer Plus"

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    buzzbuzz, i tried what you said to do, and i am getting some other errors now.

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int num;
    
    double mySquareRoot(int)
    {
        return sqrt(num);
    }
    
    int main()
    {
    	
        do
        {
            cout << "Enter a number (a double): ";
            cin >> num;
            cout << "The square root is " << mySquareRoot(num);
        } while (num != 0);
    }
    and the errors are now:
    Code:
    1>c:\documents and settings\bruce\my documents\visual studio 2008\projects\ass25\ass25\ass25.cpp(9) : error C2668: 'sqrt' : ambiguous call to overloaded function
    1>        c:\program files\microsoft visual studio 9.0\vc\include\math.h(581): could be 'long double sqrt(long double)'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\math.h(533): or       'float sqrt(float)'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\math.h(128): or       'double sqrt(double)'
    1>        while trying to match the argument list '(int)'
    this is the error that i have typically been getting for the duration of trying to do this.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You are passing an int to sqrt(). sqrt() doesn't take int, it takes either double or float. So the compiler must first convert the int to a double or float. It doesn't know which one to pick, so it yells.

    The solution is to pass it a double, since it expects a double.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89
    So num should be double and the lines should be:

    Code:
    double num;
    
    double mySquareRoot(double)
    ?

    I didn't get any errors using code::blocks doing it the other way. And the above also works ok through it......try that.
    Any help I give may be classified as:
    The Blind leading the Blind...
    Currently working through:
    "C++ Primer Plus"

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    got it working, thanks a lot. the final output is

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    double num;
    
    double mySquareRoot(double)
    {
        return sqrt(num);
    }
    
    int main()
    {
    	
        do
        {
            cout << "Enter a number (a double): " << endl;
            cin >> num;
            cout << "The square root is " << mySquareRoot(num) << endl;
    		
        } while (num != 0);
    }
    the only problem is, that once a 0 is entered, the program should stop immediately. it shouldnt read "the square root is 0" and then stop.
    i tried a couple things to help this, but neither worked.

    Code:
    if (mySquareRoot(num) == 0)
    break;
    ----AND----
    if (num == 0)
    break;
    both of these dont give me what i want. should i change the loop into something thats not do..while?

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    That's still totally wrong. In fact your edits all seem to have involved improving one part and making something else worse.

    Seeing as how it can't be an actual assignment to just make a program so simple. I'm just going to show you the corrected code.
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    double mySquareRoot(double number)
    {
        return sqrt(number);
    }
    
    int main()
    {
        double num;
        do
        {
            cout << "Enter a number (a double): " << endl;
            cin >> num;
            cout << "The square root is " << mySquareRoot(num) << endl;	
        } while (num != 0);
    }
    Note that I've purposely changed num to number in two places. This was not necessary to change, but it is to show you that there are in fact two different variables involved here, not just the one.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Quote Originally Posted by kbpsu View Post
    Write a program with a function called mySquareRoot. In main, prompt the user for a number (a double), then – by calling the mySquareRoot function – calculate and display the square root of the number. Use the standard cmath function to calculate the square root. Stop processing numbers when the user enters a zero.
    Unless you are only paraphrasing the assignment description, the assignment is asking you to "calculate and display" in the mySquareRoot() function. This means the code you have is incorrect as you are only "calculating" it in your function.

    Also, and again unless you are only paraphrasing, the description says to stop when a 0 is entered, it doesn't say stop immediately or stop without displaying the square root of 0.
    Last edited by jEssYcAt; 04-02-2009 at 12:56 AM.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    that was the full assignment description. the program works fine for everything except the condition in which it needs to stop immediately.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Aaaargh, should have read more of the original post!
    Sheesh, at this rate they'll have you calculating the area of a rectangle six-months from now.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. Sqrt() Function
    By tmoney$ in forum C Programming
    Replies: 4
    Last Post: 04-22-2003, 04:31 PM