I have been trying to take the square root of a number by doing this:
AndCode:a = Sqr(25);
I get the following error:error C2065: 'Sqr' : undeclared identifierCode:a = Sqrt(25);
and error C2065: 'Sqrt' : undeclared identifier....
What is wrong?
Printable View
I have been trying to take the square root of a number by doing this:
AndCode:a = Sqr(25);
I get the following error:error C2065: 'Sqr' : undeclared identifierCode:a = Sqrt(25);
and error C2065: 'Sqrt' : undeclared identifier....
What is wrong?
Include the math.h header file.
Code:#include <math.h>
...
double root = sqrt(nymber);
It's in the math.h system header, and since it's a library function it's all lower case:
Code:float x = sqrt(5.4f);
I'll let you have that one ;)
parksie 1 - 1 Vlatko
:D:D
Now I get the error:
C:\Programmer\Microsoft Visual Studio\MyProjects\Equation\equation.cpp(41) : error C2361: initialization of 'x' is skipped by 'default' label
C:\Programmer\Microsoft Visual Studio\MyProjects\Equation\equation.cpp(37) : see declaration of 'x'
What's at those lines?
Code:switch(choice){
case 1 :
// Do actions
break;
case 2 :
cout<<"Input a: ";
cin>>a;
cout<<""<<endl;
cout<<"Input b: ";
cin>>b;
cout<<""<<endl;
cout<<"Input c: ";
cin>>c;
d = b * b - 4 * a * c;
cout<<d<<endl;
double x = sqrt(d);
l1 = -b - x / 2 * a;
l2 = -b + x / 2 * a;
cout<<""<<endl;
cout<<"L1: " <<l1<<endl;
cout<<"L2: " <<l2<<endl;
break;
default:
cout<<"Choose either 1 or 2"<<endl;
}
You'll need to put the definition for double x before the switch block.
Now I get the following error:
:\Programmer\Microsoft Visual Studio\MyProjects\Equation\equation.cpp(16) : error C2086: 'x' : redefinition
C:\Programmer\Microsoft Visual Studio\MyProjects\Equation\equation.cpp(40) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
C:\Programmer\Microsoft Visual Studio\MyProjects\Equation\equation.cpp(41) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
C:\Programmer\Microsoft Visual Studio\MyProjects\Equation\equation.cpp(49) : error C2361: initialization of 'x' is skipped by 'default' label
C:\Programmer\Microsoft Visual Studio\MyProjects\Equation\equation.cpp(37) : see declaration of 'x'
Code:double x;
switch(choice){
case 1 :
// Do actions
break;
case 2 :
cout<<"Indtast a: ";
cin>>a;
cout<<""<<endl;
cout<<"Indtast b: ";
cin>>b;
cout<<""<<endl;
cout<<"Indtast c: ";
cin>>c;
d = b * b - 4 * a * c;
cout<<d<<endl;
double x = sqrt(d);
cout<<x<<endl;
l1 = -b - x / 2 * a;
l2 = -b + x / 2 * a;
cout<<""<<endl;
cout<<"L1: " <<l1<<endl;
cout<<"L2: " <<l2<<endl;
break;
default:
cout<<"Choose either 1 or 2"<<endl;
}
Obviously you need to take out the second definition :rolleyes:
Try initialising it to 0 at the start, though.