Garbage Value when referencing out of main function

I'm writing a function that's goal is to take an input of a radius and degrees and it outputs the co-ordinates in radians. when i return the value, it gives me the correct radians value, but then some incredible small number right after it in the same value. I've tried using a debugger and it doesn't output the garbage value with it, what am i doing wrong?

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
  #include <iostream>
#include <cmath>

using namespace std;

// FUNCTION PROTOTYPE FOR degrees2radians
double degrees2radians(double & deg);

// FUNCTION PROTOTYPE FOR compute_coord
double compute_coord(double r, double angr, double & x, double & y);

// DO NOT MODIFY THE MAIN ROUTINE IN ANY WAY
int main()
{
  double angle_degrees(0.0), angle_radians(0.0), radius(0.0);
  double coord_x(0.0), coord_y(0.0);

  // Read in polar coordinates
  cout << "Enter radius: ";
  cin >> radius;

  cout << "Enter polar angle (degrees): ";
  cin >> angle_degrees;

  // Convert degrees to radians
  angle_radians = degrees2radians(angle_degrees);

  cout << angle_radians << endl;

  // Compute Cartesian (x,y) coordinates
  compute_coord(radius, angle_radians, coord_x, coord_y);

  // Output Cartesian coordinates
  cout << "Cartesian coordinates: ";
  cout << "(" << coord_x << "," << coord_y << ")" << endl;

  return 0;
}

// DEFINE FUNCTION degrees2radians here:
double degrees2radians(double & deg)
{ cout << deg * M_PI / 180;}
// DEFINE FUNCTION compute_coord here:
double compute_coord(double r, double angr, double & x, double & y)
{
  x = r * cos(angr);
  y = r * sin(angr);

}
The

cout << angle_radians << endl;

is just my way of testing the output and seeing the garbage value
Perhaps you either need to increase your compiler warning level, or listen to your compiler. Here is what the online compiler says about your code:

In function 'double degrees2radians(double&)': 42:28: warning: no return statement in function returning non-void [-Wreturn-type] In function 'double compute_coord(double, double, double&, double&)': 49:1: warning: no return statement in function returning non-void [-Wreturn-type]
I’m sorry, my understanding of c++ is very limited because I’m in an intro level class. What does it mean to increase the warning level? And so far I’m not having a problem with the degrees2radians function.
When I input 35 for the angle, the first function takes it in and outputs a number like .6132563.457635e-547.
The correct number is there (the .63425... whatever) but there is a garbage value stored after it (the 3.234214e-547 whatever it is)
And so far I’m not having a problem with the degrees2radians function.

Yes you are, you just don't realize it yet. You promised the compiler you would return a value from that function so you must return a value from that function. By the way your main() is trying to use the value that should be returned which is probably why you're getting the "garbage" you're complaining about.

What about your compute-coord() function?

What does it mean to increase the warning level?

It means that you need to read the documentation for your compiler and IDE and make sure the compiler is generating useful warning and error messages. Your compiler should be giving you several warnings that need to be fixed.

I'm running my programs in fastx2 x-term for college, i dont think i have any control over the compiler.
By the way your main() is trying to use the value that should be returned which is probably why you're getting the "garbage" you're complaining about.


is that referencing me using pass by reference for
double degrees2radians(double & deg);
instead of pass by value? because i tried both and neither work
Enter radius: 5
Enter polar angle (degrees): 35
0.6108653.11265e-317
Cartesian coordinates: (5,1.55632e-316)

this is what the output looks like (now that im back at my computer).
its holding both 0.610865 and 3.11265e-317 when it should only be holding 0.610865. i know i must be doing something wrong because that's how coding works, i just dont see whats wrong.

is that referencing me using pass by reference for

No, it has nothing to do with a parameter, it's talking about the return value. double degrees2radians(double deg);
Sorry, that was a misunderstanding on my part, degrees2radians is definitely what i'm having problems with, i mixed up my functions. I'm trying to pass angle_degrees through the function, i have that variable referenced as deg, and it's going through the function and spitting out that value with two values attached to it. That is what i understand.

What i'm understanding from what you're saying is that the main function is trying to pass the wrong variable? if that's the case then there's something else wrong because the main is supposed to be a (correct) template that we aren't suppose to modify.
What i'm understanding from what you're saying is that the main function is trying to pass the wrong variable?

No, that's not at all what I'm saying. I'm saying you are not returning a value from that function, that is the problem. The function is the problem not main().
1
2
3
4
5
6
double degrees2radians(double & deg){
   return 42;
}

angle_radians = degrees2radians(angle_degrees);
//now angle_radians has 42 
Ok, i understand what you're saying now. Thank you for taking this slow and working this through with me. This is my first time coding and im learning form a graduate student who doesnt teach very well so i have to learn from textbooks and actually doing the codes themselves and i get tripped up sometimes.

if i write the code as

1
2
3
4
5
6
7
8
double degrees2radians(double & deg){
   return deg;
}

cout << enter degrees
cin >> deg

angle_radians = degrees2radians(angle_degrees);


would that output the deg in radians after going through the function at the bottom?
No, you probably need to do some kind of calculation to get the radians, no?

By the way why are you passing deg by reference? You may want to pass that variable by value so if you make any changes to it in the function it doesn't alter the value in the calling function.

Ohohoh, i see now, i understand. Thank you so much for the help!!
The function under the main didnt give the value back (or return) and thus a garbage value would be stored. When i did cout <<, it didnt override the garbage value, it just put it in tangent with it. Using the return command would output the value to be stores by the main function.
Thank you (again) for being patient with me
Last edited on
This ended up being my final code that works

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
#include <iostream>
#include <cmath>

using namespace std;

// FUNCTION PROTOTYPE FOR degrees2radians
double degrees2radians(double deg); 

// FUNCTION PROTOTYPE FOR compute_coord
double compute_coord(double r, double angr, double & x, double & y);

// DO NOT MODIFY THE MAIN ROUTINE IN ANY WAY
int main()
{
  double angle_degrees(0.0), angle_radians(0.0), radius(0.0); //angle_degrees is the user inputted angle. angle_radians is the angle of the user input but in radians, radius is the length of the vector that the user inputs.
  double coord_x(0.0), coord_y(0.0); //coord_x is the x coordinate value on the cartesian plane of the endpoint of the radius, and y_coord is the y value on the cartesian plane of the enpoint of the entered radius

  // Read in polar coordinates
  cout << "Enter radius: ";
  cin >> radius;

  cout << "Enter polar angle (degrees): ";
  cin >> angle_degrees;

  // Convert degrees to radians
  angle_radians = degrees2radians(angle_degrees);

  // Compute Cartesian (x,y) coordinates
  compute_coord(radius, angle_radians, coord_x, coord_y);

  // Output Cartesian coordinates
  cout << "Cartesian coordinates: ";
  cout << "(" << coord_x << "," << coord_y << ")" << endl;

  return 0;
}

// DEFINE FUNCTION degrees2radians here:
double degrees2radians(double deg)
{ return deg * M_PI / 180;}
// DEFINE FUNCTION compute_coord here:
double compute_coord(double r, double angr, double & x, double & y)
{
  x = r * cos(angr);
  y = r * sin(angr);

}


It was just that return at the end of it, so simple, i just didnt know/ forgot about it!
Since you're not using, and are not returning anything from compute_coord() you should probably change the return type to a void.

Topic archived. No new replies allowed.