Can't get this loop working!

***I got the functions working, but the loop still isn't. Is there a type of loop that will terminate as soon as the user tells it to instead of executing one more time and then closing? A loop is required for some reason.***

Professor's instructions for the loop: Your main program should contain a loop, so you can run the program until you choose to quit. For example:

do
{
…..

cout << "Do you wish to continue (Y or N) : ";

cin >> response;

response = toupper (response);

} while (response == 'Y');


My program's do while loop runs one more time before closing if the answer is N and I want it to close immediately instead. Also, the code says the shape is not a triangle no matter what numbers I put in. I'm so lost. I know they're logical errors, the program technically runs just fine, but I can't figure out what the errors are.

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
#include <iostream>
#include <string>
using namespace std;

enum triangle {scalene, isosceles, equilateral, notriangle};

void getlengths(double& length1, double& length2, double& length3);
void calcshapes(double length1, double length2, double length3, triangle& shape);
void displayshape(triangle shape);

//Call each function and keep program going according to user command via loop.
int main()
{
	triangle shape;
	double length1, length2, length3;
	char response;



	do
	{
		cout << "Do you wish to continue? (Y/N) ";
		cin >> response;
		response = toupper (response);	
		getlengths(length1, length2, length3);
		calcshapes(length1, length2, length3, shape);
		displayshape(shape);
	}
	while (response == 'Y' || response == 'y');

	system("pause");
	return 0;




}

//Obtain length of each side via user input.
void getlengths(double& length1, double& length2, double& length3)
{
	cout << "Length of the first side: " << endl;
	cin >> length1;
	cout << "Length of the second side: " << endl;
	cin >> length2;
	cout << "Length of the third side: " << endl;
	cin >> length3;
}

//Use relationship between side lengths to determine type of triangle.
void calcshapes(double length1, double length2, double length3, triangle& shape)
{
	if (!length1 + length2 > length3 || !length1 + length3 > length2 || !length2 + length3 > length1)
	{
		shape = notriangle;
	}
	else if (length1 == length2 && length1 == length3)
	{ 
		shape = equilateral;
	}
	else if (length1 == length2 || length1 == length3 || length2 == length3)
	{ 
		shape = isosceles;
	}
	else
		shape = scalene;

}

//Display the shape determined by prior function.
void displayshape(triangle shape)
{
	if (shape = notriangle)
		{
			cout << "The figure is not a triangle.";
		}
	else if (shape = equilateral)
		{
			cout << "The figure is an equilateral triangle.";
		}
	else if (shape = isosceles)
		{
			cout << "The figure is an isosceles triangle.";
		}
	else if (shape = scalene)
	{
		cout << "The figure is a scalene triangle.";
	}
}
Last edited on
= != ==
You're using the assignment operator (=) instead of the compare if equal operator (==) in the "if else if" part of your "displayshape()" function, this is why you're always getting "The figure is not a triangle." for your output. IMHO a switch case would look better here, but that's mostly aesthetic and something for you to decide later after you get everything else working.

Also revisit the "calcshapes()" function. There are a lot more scenarios where three random line segments don't form a triangle then there are otherwise. So maybe your should reconsider what your testing for and what your default case should be. Also, once a function like this has found what it is looking for it should return so as not to waste time needlessly testing the other possibilities.
Last edited on
This line looks suspicious:
if (!length1 + length2 > length3 || !length1 + length3 > length2 || !length2 + length3 > length1)

Due to operator precedence, this gets evaluated as
if ( (!length1) + length2 > length3 || (!length1) + length3 > length2 || (!length2) + length3 > length1)
which is probably not what you want.
Can't believe I missed that with operator precedence... I got the functions working thanks to the suggestions from both of you. Thanks! But the loop still isn't working. My professor specifically told us to include one so the user could keep testing other numbers (for some reason, idk what), so I can't just take it out.

As for the calcshapes function, she also told us to only use those characteristics to test the figure. She gets really peeved if we go against her instructions, even if the program would technically be better for it. Something about how a real-life employer wouldn't tolerate it.


Got the loop working! I had the continue request executing before the functions and switching them got it working normally. Thank you so much for the help!
Last edited on
Topic archived. No new replies allowed.