[Beginner] Incorrect Output for a Simple Code
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [Beginner] Incorrect Output for a Simple Code

  1. #1
    Join Date
    Sep 2012
    Posts
    3

    [Beginner] Incorrect Output for a Simple Code

    The program prompts for 3 circles' center coordinates and their radii.
    The program then prompts for a point which is then evaluated for inclusion/exclusion in the three circles' areas.

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    {
      // Variable declarations
      double x1;                 // x coordinate circle A
      double y1;                 // y coordinate circle A
      double x2;                 // x coordinate circle B
      double y2;                 // y coordinate circle B
      double x3;                 // x coordinate circle C
      double y3;                 // y coordinate circle B
      double r1;                 // radii circle A
      double r2;                 // radii circle B
      double r3;                 // radii circle C
      double X;                  // query x coordinate
      double Y;                  // query y coordinate
      double dx1 = X - x1;         // x distance between query and circle A
      double dy1 = Y - y1;         // y distance between query and circle A
      double dx2 = X - x2;         // x distance between query and circle B
      double dy2 = Y - y2;         // y distance between query and circle B
      double dx3 = X - x3;         // x distance between query and circle C
      double dy3 = Y - y3;         // y distance between query and circle C
      bool c1;                   // query point in circle A y/n
      bool c2;                   // query point in circle B y/n
      bool c3;                   // query point in circle C y/n
    
      // Prompt and read in circle A center coordinates
      cout << "Enter x and y coordinates of circle A (2 values): ";
      cin >> x1;
      cin >> y1;
    
      // Prompt and read in circle A radius
      cout << "Enter radius of circle A: ";
      cin >> r1;
    
      // Prompt and read in circle B center coordinates
      cout << "Enter x and y coordinates of circle B (2 values): ";
      cin >> x2;
      cin >> y2;
    
      // Prompt and read in circle B radius
      cout << "Enter radius of circle B: ";
      cin >> r2;
    
      // Prompt and read in circle C center coordinates
      cout << "Enter x and y coordinates of circle C (2 values): ";
      cin >> x3;
      cin >> y3;
    
      // Prompt and read in circle C radius
      cout << "Enter radius of circle C: ";
      cin >> r3;
    
      // Prompt and read in query point
      cout << "Enter x and y coordinates of query point (2 values): ";
      cin >> X;
      cin >> Y;
    
      // Determine location of query point relative to the circles  
      if ((dx1*dx1)+(dy1*dy1) <= (r1*r1)) { 
          c1 = 1;
      }
        else {
          c1 = 0;
        }
      if ((dx2*dx2)+(dy2*dy2) <= (r2*r2)) {
          c2 = 1;
      }
        else {
          c2 = 0;
        }
      if ((dx3*dx3)+(dy3*dy3) <= (r3*r3)) {
          c3 = 1;
      }
        else {
          c3 = 0;
        }
    
      // Determine whether circles contain query point
      if (c1 == 0 && c2 == 0 && c3 == 0) {
          cout << "No circle contains point (" << X << "," << Y << ")." ;
        }
      else if (c1 == 1 && c2 == 0 && c3 == 0) {
          cout << "Circle A contains point (" << X << "," << Y << ")." ;
        }
      else if (c1 == 0 && c2 == 1 && c3 == 0) {
          cout << "Circle B contains point (" << X << "," << Y << ")." ;
        }
      else if (c1 == 0 && c2 == 0 && c3 == 1) {
          cout << "Circle C contains point (" << X << "," << Y << ")." ;
        }
      else if (c1 == 1 && c2 == 1 && c3 == 0) {
          cout << "Circles A and B contain point (" << X << "," << Y << ")." ;
        }
      else if (c1 == 1 && c2 == 0 && c3 == 1) {
          cout << "Circles A and C contain point (" << X << "," << Y << ")." ;
        }
      else if (c1 == 0 && c2 == 1 && c3 == 1) {
          cout << "Circles B and C contain point (" << X << "," << Y << ")." ;
        }
      else cout << "Circles A, B and C contain point (" << X << "," << Y << ")." ;
    }
    For circles A(0,0) r=5, B(0,0) r=5, C(0,0) r=5, Point (-10,-10), I somehow get the output "Circles A, B, and C contain point (-10,-10). This happens for all values I have tried, whether it is a valid output statement or not. What am I doing wrong?

  2. #2
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,110

    Re: [Beginner] Incorrect Output for a Simple Code

    If all of the comparisons fail, your last else-statement is entered and that is the output you get.
    Looking at your logic I assume that you are missing a comparison for c1, c2 and c3 == 1. If you add that and change your last else-statement to output something like "no circle contains point ..." you should be alright.

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,568

    Re: [Beginner] Incorrect Output for a Simple Code

    Look at the location in your code of these statements:
    Code:
      double dx1 = X - x1;         // x distance between query and circle A
      double dy1 = Y - y1;         // y distance between query and circle A
      double dx2 = X - x2;         // x distance between query and circle B
      double dy2 = Y - y2;         // y distance between query and circle B
      double dx3 = X - x3;         // x distance between query and circle C
      double dy3 = Y - y3;         // y distance between query and circle C
    The variables on the right hand side of those statements have not been
    given values yet.

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,013

    Re: [Beginner] Incorrect Output for a Simple Code

    Quote Originally Posted by Gr33nMachine View Post
    Code:
    int main()
    {
      // Variable declarations
      double x1;                 // x coordinate circle A
      double y1;                 // y coordinate circle A
      double x2;                 // x coordinate circle B
      double y2;                 // y coordinate circle B
      double x3;                 // x coordinate circle C
      double y3;                 // y coordinate circle B
      double r1;                 // radii circle A
      double r2;                 // radii circle B
      double r3;                 // radii circle C
      double X;                  // query x coordinate
      double Y;                  // query y coordinate
      double dx1 = X - x1;         // x distance between query and circle A
      double dy1 = Y - y1;         // y distance between query and circle A
      double dx2 = X - x2;         // x distance between query and circle B
      double dy2 = Y - y2;         // y distance between query and circle B
      double dx3 = X - x3;         // x distance between query and circle C
      double dy3 = Y - y3;         // y distance between query and circle C
    Did you step through your code with the debugger?
    What is the value of dx1 after these lines?
    You don't assign to dx1 afterwards, so this value will be used in the comparisons in your if-else-tree.

    C++ is not mathematics. You don't write equations in C++ code, you write assignments. "dx1 = X - x1;" means assign the result of the expression X - x1 to the variable dx1. That value isn't going to change afterwards if X or x1 change. The value of dx1 only changes when you assign to it.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  5. #5
    Join Date
    Sep 2012
    Posts
    3

    Re: [Beginner] Incorrect Output for a Simple Code

    Ahh... I changed the variable declarations for dx1-3 until after they are prompted for and I'm getting the output I'm looking for, thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center