Reputation: 7
I'm sorry to be taking up some of your valuable time. I'm just a beginner programmer and was trying to compile a relatively simple code (for you guys). In my opinion, I have got my functions right, but my function 'output' 's declaration is causing some issues and I'm finding it difficult to find out what did I do wrong. There is a warning indicated by the compiler in lines 41 and 50 and I have highlighted it in my program. I'd really appreciate it if you could point out my error.
#include<simplecpp>
#include<cmath>
#define pi 3.1415926535897932384626433832795
class calculation{
private:
int i=1;
double t0=1;
double t1,R,sum=1;
double precision,degrees,radian,cosx;
public:
void getValue();
void radians();
double cosf(double radian);
void output();
};
void calculation::getValue(){
cout<<"Enter Angle: ";
cin>>degrees;
cout<<"Enter Precision: ";
cin>>precision;
}
void calculation::radians(){
degrees=abs(degrees);
radian=(degrees*pi)/180;
}
double calculation::cosf(double radian){
{
do{
R=-(radian*radian)/(2*i-1)/(2*i);
t1=R*t0;
sum=sum+t1;
t0=t1;
i++;
}
while(abs(t1/sum)>precision);
return sum;
}
}
void calculation::output(){
double cosx = cosf(radian);
cout<<"cosine of the angle will be: "<<cosx;
}
int main(){
calculation o1;
o1.getValue();
o1.radians();
double radian=0;
o1.cosf(radian);
o1.output();
}
EDIT: I made the necessary changes (as suggested by @NotAProgrammer), but while compiling, cos x is still showing the value as 1. Could you please help me out?
Upvotes: 0
Views: 198
Reputation: 598
There are several issues in your code. First of all, main program { ... }
is not a valid entry to a C++ program (which I assume you meant to use as the main function). Every C++ program must contain an int main() { }
function which is the start point. So you would have to rewrite it as following:
int main()
{
calculation o1;
o1.getValue();
o1.radians();
o1.cosf(a); //Function uses an undeclared variable
o1.output();
}
Secondly, your output issue is that in the calculation::output()
you are using a local uninitialised variable a
which you use to calculate the cosf
. There are rules in C++ governing how and if variables get initialised if they are not provided an initial value, but they are complex and honestly it is easier to just always give them a (sensible) initial value. I assume that the variable x
in calculation
class is what represents the radians? In that case you have to feed the object's x
variable to the function. Since it is a member function you have access to its private members and can just refer to x. You are also calling cosf()
twice, once in the main part of the program (where you ignore its output) and once in the output()
- is this a desired functionality? Not only that, in output()
your cosf()
function returns a double
but you assign it to a float
, meaning potential loss of precision.
Thirdly, in your getValue()
function you read in x
as the angles, but then in radians()
function you are overwriting your degrees value with the radians. This is not recommended - how would you know at any given point in time whether your variable contains the value in degrees or radians? I would also name your variables differently - why name degrees as x
and precision as n
? Just call them degrees
and precision
. This makes your code much clearer to read without having to delve deep into the functions to understand what these variables are assigned.
If you want to keep using this code you can change the output function to this, assuming x holds the radians, and go from there.
void calculation::output() {
double cosx = cosf(x); //This is the calculation object variable that holds the radians
cout << "cosine of the angle will be: " << cosx;
}
Generally for what you are trying to do, I do not see the need for object-oriented approach. You could instead write a number of free functions that take angles and return radians, and cosf function that takes radians and returns whatever the sum is etc.
Upvotes: 1