Fix my almost-finished square root code?

I'm trying to design a code that will tell you whether your number is a perfect square or not. I've gotten kind lost with it and am not really sure where to go. I have to use a bool function as well so I've been trying to design it so if theres a remainder of anything greater than 0 it'll output false. My logic may be wrong any suggestions?

/*Write a Boolean function that accepts a positive integer parameter from
 the caller and returns true if the integer is a perfect square and false 
 if not.  Test your function by writing a main that reads a positive integer,
 calls the functions to see whether it is a perfect square, and outputs an
 appropriate message.  Note that all input and output are to be done in 
 main, not in the function.
 */
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

bool square_root (int n)
{ 
   int k = (sqrt (n));
   double kk = (sqrt (n));
   double result = k -kk;
   while (result > 0)
         {return true;
         }
   return false;
}

int main()
{
    int n;
    cout << "Please enter a number and I will determine whether it is a" << endl;
    cout << "perfect square or not: " << endl;
    cin >> n;
    if (square_root(true))
       cout << n << " is a perfect square" << endl;
    else
        cout << n << " is not a perfect square" << endl;
    system ("pause");
    return 0;
}
I would have named the function is_perfect_square(), since the purpose of the function is to determine if its argument is a perfect square.

You don't need a loop (or any other kind of branch) to return true or false. Just return (result > 0);

The int vs double trick is an interesting idea, but you don't actually have to call sqrt (n) twice. Just do it once, assigning the result to a double (kk). Then set result = kk - (int)kk;

Hope this helps.
Topic archived. No new replies allowed.