Please help me create a function first time.

This is my first time creating a function and I am not sure I understand it all and the return isnt working. If anyone could give me some pointers that would be great.




#include <iostream>
#include <string>
#include <cmath>
using namespace std;

// this is my first created function
double doPythag(double c, double a, double b)
{
c = (sqrt(pow(a, 2.0) + (pow(b, 2.0))));
return c;
}

int main()
{
string answer = "Yes";
double biggest = -1, a, b, c;
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 ";
cout << c << endl; // keeps saying c is undefined
cout << "Do you want to continue (\"Yes\" or \"No\")?";
cin.ignore();
getline(cin, answer);
}
if (biggest > -1)
{
cout << "The largest answer for side c was " << biggest << endl;
}
}
You never called doPythag to compute c.
Hmm, you do not define c anywhere.

You define a variable called biggest and assign values that does not exist to it, i think what you meant was something like this.

double biggest = -1;
double a, b, c;

EDIT: Im sorry, tired, hahaha!
Last edited on
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
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

// this is my first created function
//double doPythag(double c, double a, double b)
double doPythag(double a, double b)
{
  double c = (sqrt(pow(a, 2.0) + (pow(b, 2.0))));
  return c;
  // Or
  // return (sqrt(pow(a, 2.0) + (pow(b, 2.0))));
}

int main()
{
  string answer = "Yes";
  double biggest = -1, a, b, c;
  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 ";
    c = doPythag(a,b);
    cout << c << endl; // keeps saying c is undefined
    cout << "Do you want to continue (\"Yes\" or \"No\")?";
    cin.ignore();
    getline(cin, answer);
  }
  if (biggest > -1)
  {
    cout << "The largest answer for side c was " << biggest << endl;
  }
} 


You never do anything with biggest, but I suppose one day you shall :)
Topic archived. No new replies allowed.