Can anyone help with pythagorean equation program?

Ok I am trying to right this program that calculates the largest side of a triangle, I got that part. The problem comes from the fact that when you continue to enter "yes" in the program you are still asked the sides for a, c. Well when you type in "no" I would like the program to show you. I dont want to use any functions on this just counters or something of the like. So program asks user for a and b until user types NO as I said earlier and which ever calculated answer is the largest it displays the largest C answer.

cout << "The largest answer for side c is " << //then show the answer of the largest of the answers given while user has been using the program. Here is a sample of my code.

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
double a, b, c;
string answer="Yes";

while (answer == "Yes")
{
cout << "Please enter the length of side a: ";
cin >> a;
cout << "Please enter the length of side b: ";
cin >> b;

cout << "Side c is " << (sqrt(pow(a, 2.0) + (pow(b, 2.0)))) << endl;


cout << "Do you want to continue (\"Yes\") or (\"No\")";
cin >> answer;

}
}
Initialize a variable of type double at the start called 'biggest' or whatever you want to call it.

Then after every time you print the side c, simply check to see if biggest is smaller than c. If it is then set biggest to c i.e

if(biggest < c)
biggest = c;

Hope this helps and was what you were asking for

Andy
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
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
  double a, b, c, largest_c=0; // create and initialize a new label
  string answer="Yes";
  
  while (answer == "Yes")
  {
    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)))); // calculate c
    cout << "Side c is " << c << endl; // replace the equation with c
    
    if (c > largest_c) largest_c = c; // if we have a new large value, save it in c
    
    cout << "Do you want to continue (\"Yes\") or (\"No\")";
    cin >> answer;
  }
  
  cout << "Largest hypotenous was " << largest_c << endl; // output that largest value
}
Last edited on
You can do what you are describing with dynamic memory. I would also recommend you organize the users responses into objects so that it is easier to manage. At the very least you need an array to store the users responses.
Topic archived. No new replies allowed.