Apr 29, 2014 at 9:33pm UTC
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)
Apr 29, 2014 at 9:34pm UTC
http://www.cplusplus.com/reference/cmath/pow/
Are you including the appropriate library/header?
Apr 29, 2014 at 9:42pm UTC
What you mean
<cmath>
or something else?
Apr 29, 2014 at 9:47pm UTC
yes, put in #include <cmath>
Apr 29, 2014 at 9:48pm UTC
Yes, I was assuming you weren't including the header since you've gotten the error when applying two doubles as arguments.
Apr 29, 2014 at 9:50pm UTC
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 Apr 29, 2014 at 9:56pm UTC
Apr 29, 2014 at 9:53pm UTC
You're trying too hard =D
result = pow((double ) firstnum, (double ) secondnum);
Apr 29, 2014 at 9:58pm UTC
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?
Apr 29, 2014 at 10:03pm UTC
At the command, console, or terminal:
Enter:
gcc --version
I'm on 4.7.1 and I've got no errors.
Apr 29, 2014 at 10:03pm UTC
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 Apr 29, 2014 at 10:05pm UTC
Apr 29, 2014 at 10:04pm UTC
ok I'll download it. Thank you!
Apr 30, 2014 at 8:24pm UTC
hello again, does anybody know how make sqare root?
Apr 30, 2014 at 8:35pm UTC
umm num^0.5 for square root ?
Apr 30, 2014 at 9:53pm UTC
ok thank you may you give me example on C++?