Squaring numbers

Jul 11, 2014 at 5:30pm
Still practicing basic code, wrote something to square a number inputted by the user, but some values (e.g. 5, 10, 11, 13, 15, 20) come out as the squared number -1, any help would be very welcomed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cmath>

using namespace std;

int squareNumber ()
{
    cout << "Enter number to be squared: " << endl;
    int x;
    cin >> x;
    return pow(x,2);
}

int main ()
{
    cout << "Your number squared is: " << squareNumber () << endl;
    return 0;
}
Jul 11, 2014 at 5:48pm
but some values (e.g. 5, 10, 11, 13, 15, 20) come out as the squared number -1

That's probably due to truncating the floating-point value when it is converted to an integer. Why does this matter? Well, the pow() function typically will calculate the logarithm of x, multiply that by the required power, and then calculate the antilogarithm. Thus the result may be approximate, such as 24.99999995 or something.

At any rate, all that number-crunching is a huge overkill when all that is required is to multiply x by itself.
Last edited on Jul 11, 2014 at 5:49pm
Jul 11, 2014 at 5:52pm
I was just trying to be purposely awkward to aid the learning process, thanks :)
Jul 11, 2014 at 6:23pm
CodeWizKid wrote:
I was just trying to be purposely awkward
Well you did a good job at making it awkward when you did
Your number squared is: Enter number to be squared: 10
100
I think it would look much more cleaner like
Please enter a number to be squared: 10
10 squared is 100


You could do this by something like:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
    std::cout << "Please enter a number to be squared: ";
    int numberToBeSquared = 0; //I prefer to initialize
    std::cin >> numberToBeSquared; //also good to give good variable names
    std::cout << numberToBeSquared << " squared is " << numberToBeSquared * numberToBeSquared << std::endl;

    return 0;
}
Please enter a number to be squared: 10
10 squared is 100


*fixed code tags
Last edited on Jul 11, 2014 at 6:24pm
Jul 12, 2014 at 7:35am
the codes is working well to me
Jul 12, 2014 at 10:38am
Thanks very much for the advice giblit :)
Jul 12, 2014 at 10:44am
Just to be awkward again though, why would something like this not work? It says numberToBeSuared was not declared in this scope.

#include <iostream>

using namespace std;

int squareNumber ()
{
cout << "Enter number to be squared: " << endl;
int numberToBeSquared;
cin >> numberToBeSquared;
return numberToBeSquared*numberToBeSquared;
}

int main ()
{
cout << numberToBeSquared << " squared is " << squareNumber () << endl;
return 0;
}
Jul 12, 2014 at 11:12am
numberToBeSquared is defined locally in the squareNumber function - the main function won't know what it is when you try to print it out here

1
2
cout << numberToBeSquared << " squared is " << squareNumber () << endl;
return 0;
Jul 12, 2014 at 11:20am
Okay that makes sense, how do I go about fixing it?
Jul 12, 2014 at 11:29am
Declare the variable in main(), pass it to the function. return a result from that function.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;
    
int doubleNumber(int n)
{   
    return n * 2;
}

int main()
{
    int x = 0;
    cout << "Enter x value to be doubled: " << endl;
    cin >> x;
    cout << "Double " << x  << " is: " << doubleNumber(x) << endl;
    return 0;
}

Last edited on Jul 12, 2014 at 11:30am
Jul 12, 2014 at 12:21pm
you could also do this
 
squared(n)
Topic archived. No new replies allowed.