Segmentation fault on cos

Hey guys, I'm having a little trouble with my code. I'm a new programmer, and just got to math functions. I decided to try to make a little calculator kind of thing, but when I try to calculate the cosine, I get a segfault. I've looked it up and read about them, but couldn't really find anything relating to my problem. I tried to inform myself, but I just can't figure it out. Here's my code:

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <cmath>
using namespace std;
#define PI 3.14159265

int mult (int x, int y); //function declaration (there's going to be a function called mult)
int sub (int x, int y);
int div (int x, int y);
int add (int x, int y);
double cos (double x);
int tan (int x);
int sin (int x);

int main()
{
int f_choice;
int x, y;
cout<<" 1. mult \n 2. sub \n 3. div \n 4. add \n 5. cos \n 6. tan \n 7. sin \n";
cin >> f_choice;
switch ( f_choice ) {
  case 1:
    cout<<"Enter 2 numbers:\n";
    cin>> x >> y;
    mult(x,y);
    break;
  case 2:
    cout<<"Enter 2 numbers:\n";
    cin>> x >> y;
    sub(x,y);
    break;
  case 3:
    cout<<"Enter 2 numbers:\n";
    cin>> x >> y;
    div(x,y);
    break;
  case 4:
    cout<<"Enter 2 numbers: \n";
    cin>> x >> y;
    add(x,y);
    break;
  case 5:
    cout<<"Enter 1 number: \n";
    cin>>x;
    cos(x);
    break;
  case 6:
    cout<<"Enter 1 number: \n";
    cin>>x;
    tan(x);
    break;
  case 7:
    cout<<"Enter 1 number: \n";
    cin>>x;
    sin(x);
    break;
  default:
    cout<<"Error, bad input, quitting\n";
    break;
  }
}

int mult ( int x, int y ) //declare variables x and y in function mult. since this is a new function, you can declare the vars whatever you like.
{
  int z;
  z = x * y;
  cout<<x << "*" << y << "=" <<z;
}

int sub ( int x, int y )
{
    int z;
  z = x - y;
  cout<<x << "-" << y << "=" <<z;
}

int div ( int x, int y )
{
    int z;
  z = x / y;
  cout<<x << "/" << y << "=" <<z;
}

int add ( int x, int y )
{
    int z;
  z = x + y;
  cout<<x << "+" << y << "=" <<z;
}

double cos ( double x )
{
{
  double param, result;
  param = x;
  result = cos (param*PI/180);
  cout<<"Cosine of "<< param <<"degrees is "<<result;
  return 0;
}
}

int tan ( int x )
{
  double param, result;
  param = x;
  result = tan (param*PI/180);
  cout<<"Tangent of "<< param <<"degrees is "<<result;
  return 0;
}

int sin ( int x )
{
  double param, result;
  param = x;
  result = sin (param*PI/180);
  cout<<"Sine of "<< param <<"degrees is "<<result;
  return 0;
}

And here's the bit I'm having trouble with, I believe:
1
2
3
4
5
6
 
double param, result;
  param = x;
  result = cos (param*PI/180);
  cout<<"Cosine of "<< param <<"degrees is "<<result;
  return 0;

Sorry this is kind of lengthy. If anyone could point me in the right direction, I would be very grateful. I'm on the latest Linux Kernel, on Linux Mint 10, using the G++ compiler.


EDIT: I found out how to fix it. I changed the function argument type to int instead of double like so:
int cos ( int x )
And problem solved! Sorry for this guys. (:
Last edited on
Topic archived. No new replies allowed.