Column and Loop Error

Apr 16, 2018 at 5:00pm
I am having a problem of my output coming out lined up in a nice column. It's supposed to line up with each "S" in the sides. Also, if I iterate the loop another time it will count the number of triples from the past loop instead of doing it independently. For example, the first loop had 8 triples and the second on has 4. The number of triples will be 10 instead of 4. Any help?

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

int main()
{
	int limit;
	int triples = 0;
	char input;
	do
	{
		cout << endl;
		cout << "Enter upper limit for each side of the triangle: ";
		cin >> limit;
		cout << "Side 1  Side 2  Side3" << endl;

		for (int a = 0; a <= limit; a++)
		{
			for (int b = a + 1; b <= limit; b++)
			{
				for (int c = b + 1; c <= limit; c++)
				{
					if (a*a + b * b == c * c) {

						cout << a << setw(5) << b << setw(5) << c << endl;
						triples++;
					}

				}
			}
		}
			cout << "A total of " << triples << " triples were found." << endl;

			cout << "Y or y to continue, anything else quits " << endl;
			cin >> input;
	
	} while (input == 'y' || input == 'Y');
}
Last edited on Apr 16, 2018 at 5:06pm
Apr 16, 2018 at 5:36pm
You need to make sure you're resetting the value of triples to 0 at the right time - i.e. in each iteration of the appropriate loop.
Apr 16, 2018 at 5:40pm
I feel dumb. That's exactly what the problem was with the number of triples. Thank you.
Apr 16, 2018 at 5:49pm
You're welcome! It can be easy to get confused with nested loops.
Topic archived. No new replies allowed.