1. ## 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 2. mySquareRoot(num) 3. do i put that instead of the 'double mysquareroot(int num)'? 4. 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. 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. 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. 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: Originally Posted by kbpsu 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);
}```
--remove-- 8. 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. 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. 10. 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. 11. 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. 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. 13. Originally Posted by kbpsu 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. 14. that was the full assignment description. the program works fine for everything except the condition in which it needs to stop immediately. 15. 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. Popular pages Recent additions 