user3654700
user3654700

Reputation: 63

Unable to revert back to top of While loop

I'm brand new to C++ and have a question about a problem I'm working on.

The program I'm writing is designed to do the following:

  1. ask the user to enter the length of 3 sides of a triangle
  2. check that the 3 sides pass the triangle inequality theorem (the sum of any two sides must be greater than the third side)
  3. calculate the area and perimeter of the triangle

** in step 2, if the user enters side lengths that violate the triangle inequality theorem, then ask her/him to enter the side lengths again

For example, if I enter sides:

I get the desired output and am prompted to enter another set of side lengths. But, if I enter the following:

Then I get:

"The area of the triangle is: -1.#IND
The perimeter of the triangle is: 9"

"The triangle inequality theorem has been violated.
Please enter the lengths of side A, side B and side C again."

Given sides 2, 1, 1, I'm trying to output:

"The triangle inequality theorem has been violated.
Please enter the lengths of side A, side B and side C again."

... and be taken back to the prompt for entering the side lengths.

I can't see where my code is going wrong and was hoping that someone might be able to help.

Here's my code:

#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;

void calculate(double& side_a, double& side_b, double& side_c);
void inputLengths(double& side_a, double& side_b, double& side_c);
bool isValid(double& side_a, double& side_b, double& side_c);

int _tmain(int argc, _TCHAR* argv[])
{
    double side_a = 3;
double side_b = 3;
double side_c = 3;

while (isValid(side_a, side_b, side_c) == true)
    {

    inputLengths(side_a, side_b, side_c);
    calculate(side_a, side_b, side_c);

    if (isValid(side_a, side_b, side_c) == false)
    {
        cout << "\n";
        cout << "The triangle inequality theorem has been violated." << endl;
        cout << "Please enter the lengths of side A, side B and side C again." << endl;
        continue;
    }
}
return 0 ;
}

void calculate(double& side_a, double& side_b, double& side_c)
{
double perimeter = side_a + side_b + side_c ;
double semiperimeter = (side_a + side_b + side_c) / 2 ;
double area = sqrt(semiperimeter * (semiperimeter - side_a) * (semiperimeter -     side_b) * (semiperimeter - side_c));

    cout << "\n";
    cout << "Given a triangle with the following side lengths:" << endl;
    cout << "\n"; 
    cout << "Side A: " << side_a << endl;
    cout << "Side B: " << side_b << endl;
    cout << "Side C: " << side_c << endl;
    cout << "\n";
    cout << "The area of the triangle is: " << area << endl;
    cout << "The perimeter of the triangle is: " << perimeter;
    cout << "\n";
}

void inputLengths(double& side_a, double& side_b, double& side_c)
{
cout << "\n";
cout << "Please enter the length of side A: ";
cin >> side_a;

cout << "Please enter the length of side B: ";
cin >> side_b;

cout << "Please enter the length of side C: ";
cin >> side_c;
} 

bool isValid(double& side_a, double& side_b, double& side_c)
{
// use the triangle inequality theorem to test that the sum of any two sides is
       greater than the third side
if ((side_a + side_b > side_c) && (side_a + side_c > side_b) && (side_b + side_c >
    side_a))
{
    return true;
}

else
{
    return false;
}
}

Upvotes: 2

Views: 159

Answers (3)

user3654700
user3654700

Reputation: 63

I see my own mistake. I'm calculating before checking validity. The call to calculate() should be moved lower in the while loop. And, there should be a call back to inputLengths(). The code should be:

int _tmain(int argc, _TCHAR* argv[])
{
double side_a = 3;
double side_b = 3;
double side_c = 3;

while (isValid(side_a, side_b, side_c) == true)
{
    inputLengths(side_a, side_b, side_c);


    if (isValid(side_a, side_b, side_c) == false)
    {
        cout << "\n";
        cout << "The triangle inequality theorem has been violated." <<
endl;
        cout << "Please enter the lengths of side A, side B and side C
again." << endl;
        inputLengths(side_a, side_b, side_c);
    }

    calculate(side_a, side_b, side_c);

}
return 0 ;
}

Thanks to all for your generous help.

-Elizabeth C.

Upvotes: 0

user2864740
user2864740

Reputation: 61865

Let the valid variable represent isValid(side_a, side_b, side_c), where it is only possible to update the state at one location.

valid = true;
while (valid == true)      // <-- `continue`s here, checking condition
{
    valid = updateState(); // i.e. dependent upon `inputLengths`
    if (valid == false)
    {
        continue;
    }
    /* implicitly */ continue;
}

This clearly represents an issue because the code says "continue when not valid (or, always, implicitly) but only loop while valid".

That is, the loop ends immediately after it was told to "continue" in the not valid case - the code that could change the validity does not have a chance to execute again.

Instead, consider something along the lines of the following.

// Loop repeatedly until the user enters a "valid input" state
valid = false;
while (true) {             
     valid = readTriangleSides(); // Ask for the input; is it valid?
     if (!valid) {
        // Inform the user the input was invalid; implicit `continue`
     } else {
        // Valid, end the loop - this could be eliminated if the
        // loop was expressed as `while (!valid) ..`
        break;
     }
}

// Use the obtained values, now in the "valid input" state
calculateAndDisplay();

Upvotes: 1

Luc M
Luc M

Reputation: 209

What you have with A=2,B=1,C=1 is a straight line of length 2 (so a degenerate triangle) and in this case A=B+C.

When you test your values in your function "isvalid":

if ((side_a + side_b > side_c) && (side_a + side_c > side_b) && (side_b + side_c >
side_a))

you don't consider this case the degenerate case (where side_b+side_c=side_a). Therefore you must replace all occurence of ">" with ">=", as follows:

if ((side_a + side_b >= side_c) && (side_a + side_c >= side_b) && (side_b + side_c >=
    side_a))

Upvotes: 0

Related Questions