Powering

Hello, thank you in advance! May someone tell me how to power variables with other variables, I mean :

firstnumbersecondnumber

I tried with:
pow(firstnumber, seconumber), but it gives me a error:

undefined reference to pow(double, double)





closed account (j3Rz8vqX)
http://www.cplusplus.com/reference/cmath/pow/

Are you including the appropriate library/header?
What you mean
<cmath>
or something else?
yes, put in #include <cmath>
closed account (j3Rz8vqX)
Yes, I was assuming you weren't including the header since you've gotten the error when applying two doubles as arguments.
I'll give you my code
The error lines are in bold

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <math.h>
using namespace std; int main()
{
    float firstnum, secondnum;
   long long result;
    char sign;
    while(true)
    {
        cout << "first number:\n";
        cin >> firstnum;
        cout << "What do yo want to do: * / + - ^\n";
        cin >> sign;
        cout << "second number:\n";
        cin >> secondnum;
        if(sign == '*')
        {
            cout << "the result is:" << firstnum * secondnum << "\n\n";
        }
            if(sign == '+')
            {
                cout << "the result is:" << firstnum + secondnum << "\n\n";
            }
                if(sign == '-')
                {
                    cout << "the result is:" << firstnum - secondnum << "\n\n";
                }
                    if(sign == '/')
                    {
                        if(secondnum == 0)
                        {
                            cout << "invalid denominator" << "\n\n";
                        }
                        else
                        {
                            cout << "the result is:" << firstnum / secondnum<< "\n\n";
                        }
                    }
                    if(sign == '^')
                            {
                                result = (pow((double) firstnum, (double secondnum));
                                cout << result << "\n\n";
                            }
    }
return 0;
}
Last edited on
closed account (j3Rz8vqX)
You're trying too hard =D
result = pow((double) firstnum, (double) secondnum);
Sorry, but it is alraight :)
I tried it but it didn'd work again maybe my compiler is broken or something.
I'm using code blocks as a compiler.Can this be a problem?
closed account (j3Rz8vqX)
At the command, console, or terminal:

Enter:
gcc --version

I'm on 4.7.1 and I've got no errors.
be careful with your parenthesis. i just inputted your code into codeblocks and it works if you make the line:

result = pow((double) firstnum, (double) secondnum);

your parenthesis were off by a little.

fyi, your loop is infinite, the program never ends. make a false condition so it can end.
Last edited on
ok I'll download it. Thank you!
hello again, does anybody know how make sqare root?
umm num^0.5 for square root ?
ok thank you may you give me example on C++?
closed account (j3Rz8vqX)
1
2
3
4
5
result = pow((double) firstnum, 0.5);
//square root is n^(1/2)
//Where the numerator is the power, and the denominator is the roots

//Example: 5^(2/3) is the cube-root of (5^2). 

or use the following for square root: http://www.cplusplus.com/reference/cmath/sqrt/
Requires <cmath> header.

Any root beyond that you must implement yourself as in the code above.
Topic archived. No new replies allowed.