Question on another program function?

Ok I thank whomever it was that helped me out yesterday but I have one more question. I want all of the Pythagorean calculation to occur in the function. Why isnt it calculating the biggest. Is there a way to modify biggest inside the doPythag function without modifying main anymore. Comments are provided in code.

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


void doPythag()
{
	double a, b, c;
	cout << "Please enter the length of side a: ";
	cin >> a;
	cout << "Please enter the length of side b: ";
	cin >> b;      
	c = (sqrt(pow(a, 2.0) + (pow(b, 2.0))));
	cout << "Side c is ";
	cout << c << endl; 
}

int main()
{
	string answer = "Yes";
	double biggest = -1;
	while (answer == "Yes")
	{ 
		doPythag();
		cout << "Do you want to continue (\"Yes\" or \"No\")?";
		cin.ignore();
		getline(cin, answer);    //Program works up until this point
	}
	if (biggest > -1) // this part does not calculate
	{
		cout << "The largest answer for side c was " << biggest << endl;
	}
}

Last edited on
I would recommend taking the cout and cin out of your doPythag function (the way you have it before). You've made your function a lot less flexible, what if your reading points from a file and need to know their distance to the origin?

You've also made it so you have no idea what c is, so you can't update biggest.

You basically need double doPythag(double a, double b) to return c. Once it does, check if c is greater than biggest, and if it is, make biggest equal c. This comparison is all done within the while loop.
Last edited on
Topic archived. No new replies allowed.