Help with loops

So my program doesn't seem to follow the correct loops when I compare the code I wrote to the code that is actually supposed to happen. We're given a .exe file and run the file and follow all the commands then we need to replicate a code but when I try to run mine it doesn't follow as specified and I believe it is my loops. 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
*
*File: circles_solutions.cpp
*Created by: Robert Marsch
*Created on: 2/3/2014
*Synopsis: To input the coordinates of a circle and radii of three circles and *report the location of the query point relative to the circles.
*/

#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
  int xa, xb, xc, xq, xy, ya, yb, yc, yq, ra, rb, rc;
  int distancex1, distancey1, distancex2, distancey2, distancex3, distancey3;
  int d1, d2, d3;
  int k;

  cout << "Enter x and y coordinates of circle A (2 values): ";
  cin >> xa >> xb;
  cout<< "Enter radius of circle A: ";
  cin >> ra;

  cout << "Enter x and y coordinates of circle B (2 values): ";
  cin >> xb>> yb;
  cout << "Enter radius of circle B: ";
  cin >> rb;

  cout << "Enter x and y coordinates of circle C (2 values): ";
  cin >> xc >>  yc;
  cout << "Enter radius of circle C: ";
  cin >> rc;

  cout << "Enter x and y coordinate of query point (2 values): ";
  cin >> xq >> yq;
 
  distancex1 = (xq - xa);
  distancey1 = (yq - ya);
  distancex2 = (xq - xb);
  distancey2 = (yq - yb);
  distancex3 = (xq - xc);
  distancey3 = (yq - yc);

  d1 = sqrt(pow(distancex1,2)+pow(distancey1,2));
  d2 = sqrt(pow(distancex2,2)+pow(distancey2,2));
  d3 = sqrt(pow(distancex3,2)+pow(distancey3,2));

  

  while (int k=1)
 
    if((d1 <= ra) && (d2 <= rb) && (d3 <= rc))
    {
      cerr << "Circles A B and C contain point (" << xq << " ," << yq << "). " << endl;
      exit(10);
    }

    else if ((d1 <= ra) && (d3 <=  rc) && (d2 >= rb))
    {
      cerr << "Circles A and C contain point (" << xq << " ," << yq << "). " << endl;
      exit(20);
    }

    else if((d3 >= rc) && (d1 <= ra) && (d2 <= rb))
    {
      cerr << "Circles A and B contain point (" << xq << " ," << yq << "). " << endl;
      exit(30);
    }

    else if((d2 <= rb) && (d3 <= rc) && (d1 >= ra))
    {
      cerr << "Circles B and C contain point (" << xq << " ," << yq << "). " << endl;
      exit(40);
    }

    else if((d2 >= rb) && (d3 >= rc) && (d1 <= ra))
    {
      cerr << "Circle A contains point (" << xq << " ," << yq << "). " << endl;
      exit(50);
    }

    else if((d1 >= ra) && (d3 >= rc) && (d2 <= rb))
    {
      cerr << "Circle B contains point (" << xq << " ," << yq << "). " << endl;
      exit(60);
    }

    else if((d1 >= ra) && (d3 >= rc) && (d2 <= rb))
    {
      cerr << "Circle C contains point (" << xq << " ," << yq << "). " << endl;
      exit(70);
    }
   
    else if((d1 > ra) && (d2 > rb) && (d3 > rc))
    {
      cerr << "No circle contains point (" << xq << " ," << yq << "). " << endl;
      exit(80);
    }
 
return 0;
 
}
Can you show us example output of the program so we know what it is supposed to look like?


*edit

what is up with this by the way?
while (int k=1)
Last edited on
Well when the first loop is executed it should return this:
Enter x and y coordinates of circle A (2 values): 2 0
Enter radius of circle A: 3
Enter x and y coordinates of circle B (2 values): 0 2
Enter radius of circle B: 3
Enter x and y coordinates of circle C (2 values): -2 0
Enter radius of circle C: 3
Enter x and y coordinates of query point (2 values): 0 0
Circles A, B, and C contain point (0,0).

but instead my program does this and instead of saying Circles A, B, and C contain point (0,0) it says Circles B and C contain point (0,0). and it shouldn't.
Also I know what it should say because we were given the .exe file to run to show us how the output should look.

and I think I might have left that in there when I posted the code but in my actual code the while (int k=1) isn't in there
Last edited on
I think this is your problem:

cin >> xa >> xb; shouldn't that be cin >> xa >> ya;?
Topic archived. No new replies allowed.