Bolt Circle Calculator

Hi everyone, I am a first time user of these forums and joined in the hopes of getting some help with what is supposedly an easy problem which my ineptitude will not allow me to grasp. The purpose of the program I've been assigned is to calculate the precise locations of hole centers (in Cartesian coordinates) in a bolt hole pattern with certain values (specifically, bolt circle diameter, number of holes required, location of origin on x, location of origin on y, angle to first hole, angle to last hole) being inputted by the user. Now, mathematically this is simple and I could compute these locations very easily using a calculator, but I guess my chaotic mind is not well suited to the task of programming because I am at a loss.

I have written the script, which I will paste below, in Borland C++ 5.02. When compiling, I receive only one warning which is that I have assigned the variable "loop" a value which never gets used. The program itself runs fine right through the conditional "if" statement (when condition is met) but then an innumerable quantity of numbers begin flying wildly across the screen as though it is not exiting the nested loop. In the case where the "if" condition is not met, it does not even enter the second loop, it just shuts the program down.

Please, any help you can offer will be appreciated, all that I ask is that you don't answer my question with your own working version of the program unless absolutely necessary to make your point. I truly want to learn this stuff for myself, all I'm looking for are some subtle hints as to where I might be going wrong. Thanks in advance! Without further adieu, here is 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

/*Program Name: Bolt Circle Calculator
  Programmer: Jason Saville
  Date: February 16, 2011
  This program will: Accept input from the user, specifically the diameter of the
  bolt circle, number of points required, and center location, then calculate
  and print the X and Y coordinates of each point. */


  #include <stdio.h>
  #include <math.h>

  #define PI 3.14159

  main ()

  {

  int numpoints, boltdia, refx, refy, loop;
  float circrad, increment, firstangle, anglesum, locx, locy, answer, lastangle, spanangle;

  printf("Enter the diameter of the bolt circle:  ");
  scanf("%d", &boltdia);

  printf("Enter the number of points required:  ");
  scanf("%d", &numpoints);

  printf("Enter x-coordinate of reference point:  ");
  scanf("%d", &refx);

  printf("Enter y-coordinate of reference point:  ");
  scanf("%d", &refy);

  printf("Enter angle to first point in degrees:  ");
  scanf("%f", &firstangle);

  printf("Is this a full circle? (YES=1, NO=0):  ");
  scanf("%f", &answer);


  if (answer == 0)

    {printf("Enter angle to last point:  ");
     scanf("%f", &lastangle);

     spanangle = lastangle - firstangle;

     circrad = boltdia / 2;

     increment = (spanangle / numpoints);



     for (loop=1, anglesum=firstangle; loop<=numpoints, anglesum<=lastangle; anglesum+increment, loop++)

      {increment = (360 / numpoints);

       locx = (circrad * (cos(anglesum * (PI/180)))) + refx;

       locy = (circrad * (sin(anglesum * (PI/180)))) + refy;

       printf("At %f degrees, the hole center is at (%f, %f)/n", anglesum, locx,     locy);

      }
    }


  for (loop=1, anglesum=firstangle; loop<=numpoints, anglesum<=lastangle; anglesum+increment, loop++)

   {locx = circrad * (cos(anglesum * (PI/180)));

    locy = circrad * (sin(anglesum * (PI/180)));

    printf("At %f degrees, the hole center is at (%f, %f)/n", anglesum, locx, locy);

   };


  return (0);

  }
 
Last edited on
1
2
3
4
for (loop=1, anglesum=firstangle; 
  loop<=numpoints, anglesum<=lastangle;  //left-hand operand of comma has no effect (and or ? )
  anglesum+increment //statement has no effect
  , loop++)

circrad = boltdia / 2; Integer division produce an integer result.

in Borland C++ 5.02
http://www.cplusplus.com/forum/articles/36896/
Those headers are deprecated (it should be cmath and cstdio without .h )
main must return int.

PI is already defined in cmath as M_PI
You can limit the scope in c++, declaring variables as you need them for( int K=0; K<n; K++).
You may want to check iostream for i/o
Last edited on
Thank you for your response ne555. Now, when you say that the left hand operand of the comma has no effect, you are speaking of the "loop<=numpoints" line, am I correct? Why exactly does this have no effect? What I'm looking for the loop to do is calculate the location of the hole center on both x and y and to calculate that for as many holes as was inputted by the user. So, in my initializing statement, I have set the loop equal to one and wish it to repeat this loop (i.e. the operation of calculating the location on x and the location on y), adding one to the loop number each time it runs the circuit, up to the value of the number of points, which I have used as a limiting condition. I'm sorry if I'm missing the obvious, I'm just completely inept with this stuff.
http://www.cplusplus.com/doc/tutorial/operators/
The comma operator executes all the statements, but only return the value of the last one.
if( a, b ) a will be ignored so the condition is actually if( b ) (in your case only anglesum<=lastangle matters ).
Topic archived. No new replies allowed.