Loops question

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

When the code is run only the first loop is utilized and it skips the following when it should move on after the first loop commands are not met

  1. Relevant commands, code, scripts, algorithms:
    while loop, else if

  2. The attempts at a solution (include all code and scripts):

/*
*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 && d2 && d3 <= ra && rb && rc)
    {
      cerr << "Circles A B and C contain point (" << xq << " ," << yq << "). " << endl;
      exit (10);
    }

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

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

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

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

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

  else if (d1 && d3 >= ra && rc && d2 <= rb)
    {
      cerr << "Circle C contains point (" << xq << " ," << yq << "). " << endl;
      exit(70);
    }
   
  else if (d1 && d2 && d3 >= ra && rb && rc)
    {
      cerr << "No circle contains point (" << xq << " ," << yq << "). " << endl;
      exit(80);
    }
 
return 0;
 
}
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Ohio State, Columbus, OH, USA, Sharief, CSE1222

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Well I only see one loop...

First you request and obtain some values. This code is not in a loop and will only happen one time. Then we encounter while (int k=1) which is an infinite loop. It will never stop. That is sometimes ok, but probably not a great idea here. Normally a while loop would have braces to show the statements it is controlling. You have no braces, that is legal, so only one statement is controlled by your infinite loop. It is a lengthy if statement with many "else if" causes.

I guess one of the tests tested true. For each test you output something and then call exit(). A call to exit() terminates the program. So if you see any output, we expect the program to hit exit() and well, exit.

Suppose none of the tests were true. Then your infinite loop would run the same tests on the same data. If none of the tests were true the first time, they will still not be true the second time. Or the third time. Or the fourth time.... This is not a good thing.

Your infinite loop will never finish. But if it did, you would execute your "return" statement. Returning from main() is legal and will also terminate the program. Right now you can't reach that return statement.

Fair warning, I'm tired and I just glanced at your code. I haven't tried it. I could be wrong. But I don't think so. Good night... going to bed. :slight_smile:

1 Like
int k=1;
while (k==1)

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

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

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

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

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

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

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

The loop does not exit because k is never changed (or referenced) inside the loop. Your code had several problems. Note the read areas. You MUST COMPILE with WARNINGS enabled! Correct code has ZERO warnings. At a minimum, assuming the logic is correct. The code you posted could not have compiled without warnings. You still need to set k to zero somewhere in the loop code.