overloaded function with argument evaluation error
So I'm learning C++ and I have to create an overloaded function and from
that take the largest number from a set of numbers. This works with 2
numbers but when I comment out the call to the function for 2 numbers and
try the one with 3 numbers it gives me a ton of errors. I need a fresh
pair of eyes to look at my code and see what I am doing wrong.
#include <iostream>
using namespace std;
// function prototypes
double max(double numberOne, double numberTwo);
double max(double numberOne, double numberTwo, double numberThree);
int main()
{
int numberOne,
numberTwo,
numberThree;
// user input
cout << "Input number 1: " << endl;
cin >> numberOne;
cout << "Input number 2: " << endl;
cin >> numberTwo;
cout << "Input number 3: " << endl;
cin >> numberThree;
cout << "The largest of " << numberOne << " and " << numberTwo << ": "
<< max(numberOne, numberTwo) << endl;
cout << "The largest of " << numberOne << ", " << numberTwo << ", and
" << numberThree << ": " << max(numberOne, numberTwo, numberThree) <<
endl;
return 0;
}
// function declarations
double max(double numberOne, double numberTwo) {
// if number one is greater than number two return that
// otherwise return numberTwo as the greater value
if (numberOne > numberTwo) {
return numberOne;
} else {
return numberTwo;
}
}
double max(double numberOne, double numberTwo, double numberThree) {
// compare 1 and 2 to see which is greater
if (numberOne > numberTwo) {
if (numberOne > numberThree) {
// 2 is greater return it
return numberOne;
} else {
// else 3 is greater return that
return numberThree;
}
} else {
if (numberTwo > numberThree) {
// 2 is greater return it
return numberTwo;
} else {
// else 3 is greater return that
return numberThree;
}
}
}
No comments:
Post a Comment