0

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?

  • 6
    Suggestion: use an [indentation style](https://en.m.wikipedia.org/wiki/Indentation_style) that effectively shows the structure of code (the style "keep everything left-aligned" fails to do that). – pmg May 08 '20 at 10:48
  • 1
    @pmg Ok, I'll keep that in mind the next program I code. Thank you for your suggestion. – Firefox1921 May 08 '20 at 10:49
  • 5
    @Firefox1921 I think he means "pls edit your post and indent your code so it can be humanly readable" – Martin Morterol May 08 '20 at 11:03
  • 1
    you don't have to wait till next time, many IDEs and editors have an auto indentation feature which makes it as easy as clicking a button – 463035818_is_not_an_ai May 08 '20 at 11:05
  • `o1.cosf(a);` How this can compile ? there is no `a` in this scope – Martin Morterol May 08 '20 at 11:08
  • 1
    Voting to close this as a typo. You read the angle into a variable `x`, convert that to radians, and then call `cosf` with an entirely new variable `a`. The compiler is even telling you "this variable is undefined". – Botje May 08 '20 at 11:08
  • 1
    This will not compile. Other than that, in your output function you are using a local uninitialised variable `a` to feed into your `cosf` function. – NotAProgrammer May 08 '20 at 11:09
  • LOL @MartinMorterol, you have *powers*: that's what I mean :) – pmg May 08 '20 at 11:10
  • @pmg indentation edit is done. – Firefox1921 May 08 '20 at 11:11
  • @NotAProgrammer So what should I do? Should I change the variable in the output function to x? – Firefox1921 May 08 '20 at 11:13

1 Answers1

1

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.

NotAProgrammer
  • 552
  • 1
  • 5
  • 15
  • Thank you so much @NotAProgrammer for identifying my mistakes. I mostly understood what you're saying, but I didn't understand the part where you said, '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.' Could you please elaborate on that? – Firefox1921 May 08 '20 at 12:08
  • `x` is a private member variable of the class `calculation`. Therefore you can only access that variable through either member functions of the `calculation` class, or through functions that have been declared as `friend`. Because your `output()` function is a member function of `calculation`, it is allowed to read the value of the variable. This may shed some more light on it [Private and Protected Members : C++](https://stackoverflow.com/questions/224966/private-and-protected-members-c) – NotAProgrammer May 08 '20 at 12:14
  • Thanks @NotAProgrammer. I made the necessary changes in my code and have updated the same in the OP. The compiler still shows the value of cosine of x as 1. Please tell me what error I have made in the latest edit to the post. – Firefox1921 May 08 '20 at 12:37
  • You are always calling `cosf()` with `double radian=0`. As such, it will always give you `1` because cos 0 = 1. That is a local variable, you need to use the radian of the `calculation` object, namely `o1.radian`. You should read up on the scope and lifetime of variables and pick up a good introductory book to C++, I think you have bitten off a bit more than you can chew here. – NotAProgrammer May 08 '20 at 12:42
  • Ok, I'll do that @NotAProgrammer. But could you please explain this statement, 'you need to use the radian of the `calculation` object, namely `o1.radian` '. – Firefox1921 May 08 '20 at 12:49
  • You need to use the value of the variable that you are putting the result of your `radians()` function into. Not another variable with the same name. – NotAProgrammer May 08 '20 at 13:06
  • And how do I do that, @NotAProgrammer? I'm really sorry for prolonging this post. – Firefox1921 May 08 '20 at 13:14
  • You can access the member variables inside member functions by just using their name, like I did in my post where I used `cosf(x)` inside `output()`. Just replace the cosf(x) with `cosf(radians)` and remove the `double radians=0; cosf(radians)` from main. – NotAProgrammer May 08 '20 at 14:24
  • Looks like I got it! Thank you so much for your help @NotAProgrammer. I will remember your suggestions and implement them in my future code. Thank you for patiently answering all my doubts. – Firefox1921 May 08 '20 at 14:45