While Loop Error

My problem is that when someone inputs the length of three sides of a triangle and they do not produce a triangle the while loop asks for input again, but when side lengths that can produce a triangle are entered the program says a correct triangle cannot be made.

Here is the code. I can provide the other function definitions if needed. Also, for the reenter_triangle function I just pasted pasted the code from the get_input function into the reenter_triangle body because I didn't want the statement "Input the lengths of three sides of a triangle: " to be output again. Is there a better way to do this?

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
105
106
#include <iostream>		
#include <cmath>		
using namespace std;

void description();

void get_input(double& side_a, double& side_b, double& side_c);

bool isTriangle(double side_a, double side_b, double side_c);

double semiperimter(double side_a, double side_b, double side_c);

void area_perimeter(double side_a, double side_b, double side_c, double& perimeter, double& area);

void show_output(double perimeter, double area);	


int main ()
{
	char answer(0);
	double perimeter, area, side_a, side_b, side_c;
	do
	{	
		description();
		get_input(side_a, side_b, side_c);
		if (isTriangle(side_a, side_b, side_c))
		{
			area_perimeter(side_a, side_b, side_c, perimeter, area);
		}
		while (!isTriangle(side_a, side_b, side_c))
		{
			cout << "\nThe side lengths you entered cannot form a triangle.\n";
			cout << "Change one or more side lengths and try again.\n";
			get_input(side_a, side_b, side_c);
		}
		show_output(perimeter, area);
		
		cout << "Would you like to run this program again? (Y or y)\n";
		cin  >> answer;
	}
	while (answer == 'y' || answer == 'Y');

	return 0;
}

void description()	
{
  cout << "\nThis program calculates the perimeter and area of a triangle\n";
  cout << "based on user input of three side lengths.\n\n";
}

void get_input(double& side_a, double& side_b, double& side_c)		
{																
  cout << "Input the lengths of three sides of a triangle:\n";
  cin >> side_a;
  while (side_a <= 0)
  {
	  cout << "Enter a positive nonzero number";
	  cin >> side_a;
  }
  cin >> side_b;
  while (side_b <= 0)
  {
	  cout << "Enter a positive nonzero number";
	  cin >> side_b;
  }
  cin >> side_c;
  while (side_c <= 0)
  {
	  cout << "Enter a positive nonzero number";
	  cin >> side_c;
  }
  cout << "The side lengths of the three sides are: " << side_a << ", " << side_b << ", " << side_c << ".\n\n"; //Echo user input

}
  
bool isTriangle(double side_a, double side_b, double side_c)
{
	if (side_a + side_b > side_c && side_a + side_c > side_b && side_b + side_c > side_a)	
		return true;				  
	else							
		return false;
}

double semiperimter(double side_a, double side_b, double side_c)
		{
			double semiPer;

			semiPer = (side_a + side_b + side_c)/2;	
			return semiPer;							
		}

void area_perimeter(double side_a, double side_b, double side_c, double& perimeter, double& area)	
{																		
	double semiPer;

	semiPer = semiperimter(side_a, side_b, side_c);
	perimeter = side_a + side_b + side_c;
	area = sqrt (semiPer*(semiPer - side_a)*(semiPer - side_b)*(semiPer - side_c)); 
}

void show_output(double perimeter, double area)
{
	cout << "The perimeter of the triangle is " << perimeter << " units.\n";	//I wasn't sure if I should ask the user 
	cout << "The area of the triangle is " << area << " square units.\n";		//to input their units in also so I just 
}										    //wrote units  
Last edited on
I can't see because you've chopped off the head of that first function, but are you passing by reference? If not, you probably should be.

Same goes for reenter_triangle. You need to be passing by reference for the variables in main to change. Your current method operates on the value of what's passed in, not the reference of the variable itself.
Sorry about that. I will edit the post and include all my functions declarations. This is an assignment so I have to follow some rules. I was told for the perimeter and area calculation function the three side length measurements should be pass by value and the perimeter and area calculations should each be pass by reference.
My problem is that when someone inputs the length of three sides of a triangle and they do not produce a triangle the while loop asks for input again, but when side lengths that can produce a triangle are entered the program says a correct triangle cannot be made.


As iHutch105 has pointed out, void reenter_triangle(double side_a, double side_b, double side_c); passes by value, no matter what you enter inside that function, the values that were passed in, will be the same values still present in each one of those variables after that function returns, which inevitably is not a valid triangle (which is why you are asking to re-enter anyways).

Change those to references, and report back.
Last edited on
If I was told this assignment without the specific conditions from the professor I would definitely use pass by reference parameters. She said each side length should be pass by value though. She seems strict with these kinds of things. I have an example below.

I'm not good at learning from lectures so I do a lot of textbook reading. I read about preconditions and postconditions and used them in my program with my function declarations and the professor asked why I was doing that since it was never covered in class. I thought it was good technique and gave someone looking at the code a better idea of the functions.

Is there a way to do this with pass by value?

I'm thinking to maybe get an error message in the while loop and then call the reenter_triangle function if the bool function is false. I'm not sure how to do that though. If she made the assignment up herself I don't know what she had in mind for the code if she disallowed reference parameters for some arguments.
Last edited on
If I was told this assignment without the specific conditions from the professor I would definitely use pass by reference parameters. She said each side length should be pass by value though. She seems strict with these kinds of things. I have an example below.


And yet you use pass-by-reference for get_input, and reenter_triangle is just get_input with different prompts.

You could get away with replacing line 15 with:

1
2
3
	cout << "\nThe side lengths you entered cannot form a triangle.\n";
	cout << "Change one or more side lengths and try again.\n";
	get_input(side_a, side_b, side_c);


and get rid of reenter_triangle altogether.
Last edited on
I did use reference parameters for get_input and reenter_triangle. I guess I'm not sure what she meant in the assignment.

I don't know why I didn't think of just putting cout statements inside the while loop. Sometimes I think I get caught up in thinking code has to look a certain way.

Here is part of the assignment.

Write a void function that computes the area and perimeter (not the semiperimeter) of a triangle based on the length of the sides. The function should use five parameters--three value parameters that provide the lengths of the edges and two reference parameters that store the computed area and perimeter.

Your program should produce correct results for legal data and reasonable results for illegal combinations.


I think she thought people would just code cout and cin statements in main so pass by reference parameters weren't needed. I read a little on procedural abstraction and I like to have main as simple as possible. That probably doesn't matter much here though. I didn't use the reference parameters in the area_perimeter function and I thought it was okay to use them in my get_input function.

I deleted reenter_triangle and put get_input inside the while loop but when I go to re-enter a correct triangle I get an error saying "Run-Time Check Failure #3 - The variable 'area' is being used without being initialized"
Last edited on
Write a void function that computes the area and perimeter (not the semiperimeter) of a triangle based on the length of the sides.
You have done this, this is your area_perimeter function. Note
The function should use five parameters--three value parameters that provide the lengths of the edges and two reference parameters that store the computed area and perimeter.
Nowhere in these instructions does it say, if you create another custom function for getting the inputs do you also need to pass the sides as values to such function. Show me where your professor said only pass the sides by value to any function and not just that perimeter calculating one. If you cant, then you have your answer, you can in fact make use of references in your own functions.
could you by chance post your code in full?
Yea, I can post the entire code. I'll edit the first post so the thread is easier to read.

I see what you guys were saying about the pass by value parameters in the reenter_triangle function. I'm still not sure why I'm getting the error message that area is uninitialized though.

Edit: I have an exam tonight in this class so I won't be back until about 10:30, so hopefully someone can help around that time if my problem can't be fixed from what I've wrote here. This assignment is due tonight. I don't think I would get points off for this problem but I rather have everything working right.
Last edited on
I deleted reenter_triangle and put get_input inside the while loop but when I go to re-enter a correct triangle I get an error saying "Run-Time Check Failure #3 - The variable 'area' is being used without being initialized"


In the case where the first 3 sides entered cannot form a triangle, area_perimeter is not called prior to show_output, so the value of area is either undefined or inappropriate when a user must reenter the side lengths.
Damn, its due at 11 not 12 and since I the put get_input function inside the for loop I get a runtime error when a user re-enters input.

I tried putting the show_output function in the if statement after the area_perimeter function and I don't get a runtime error but the output statements aren't shown when a correct triangle is entered.
Last edited on
The solution is to do all of the input validation before calling area_perimeter, not moving show_output. You could just get rid of that whole if(isTriangle(...)) block and call area_perimeter immediately before show_output.
Topic archived. No new replies allowed.