I am not sure how to make this overloaded function work correctly.
The problem is this:
Write two overloaded functions called max that takes either two or three parameters of type double and returns the largest of them.
Write a function that calculates the portion size of ice cream a group of customers will receive. The function should receive the number of customers, and the weight of the ice cream. The function should also output the portion of each customer.
Write appropriate comments, in the form of purpose, parameters, and return for problems 1 and 2.
double max(double num1, double num2)// Takes in two double parameters.
{ // Calculates the max of two numbers
if(num1 > num2)
{
return num1; // Returns number 1 if it is the largest.
}
else
{
return num2; // Returns number 2 if it is the largest.
}
}
// Takes in three double parameters.
double max(double num1, double num2, double num3)
{ // Also calculates the max but if there is three numbers to compare.
if(num1 > num2 and num1 > num3)
{
return num1; // Returns number 1 if it is greater than number 2 and 3.
}
elseif(num2 > num1 and num2 > num1)
{
return num2; // Returns number 2 if it is greater than number 1 and 3.
}
else
{
return num3; // Returns number 3 if it is greater than number 1 and 2.
}
}
double iceCreamShare(double iceCream, int people = 1)
{
return iceCream / people;
}
int main(int argc, char** argv) {
int choice;
do{
cout << "---------------::Main Menu::---------------\n";
cout << "Please choose one of the following options.\n";
cout << "--------------1) Max Function--------------\n";
cout << "--------------2) Ice Cream-----------------\n";
cout << "--------------3) Quit----------------------\n";
cin >> choice;
switch(choice)
{
case 1:
double num1, num2, num3;
cout << "Please enter two or three numbers to find the max number:";
cin >> num1, num2, num3;
cout << max(num1, num2, num3);
break;
case 2:
double iceCream;
int people;
cout << "Enter the amount of ice cream in pints: ";
cin >> iceCream;
cout << "Enter the number of people: ";
cin >> people;
cout << "Each person should get " << iceCreamShare(iceCream, people)
<< " pint(s) of ice cream.\n";
break;
}
}while(choice != 3);
return 0;
}
I was expecting for my max function to work whether I put two or three values.
But I am not sure why it isn't working.
No matter what, my max function just outputs the first number that it receives
Thank you so much.
I've done that before, and thought I was more careful to not make that mistake.
Will that allow there to be two or three inputs and still use either of my max functions though?
I'll check right now to find out but if not please let me know.
Again, thank you.
:]
I was expecting it to work whether I put 2 or 3 numbers for my Max function,
but when I run my max function, it doesn't give me the proper value as being max.
As well, it always expects 3 inputs.
How can I make my program allow 2 or 3 inputs and actually display the larger number?
#include <cstdlib>
#include <iostream>
usingnamespace std;
double max(double num1, double num2)// Takes in two double parameters.
{ // Calculates the max of two numbers
if(num1 > num2)
{
return num1; // Returns number 1 if it is the largest.
}
else
{
return num2; // Returns number 2 if it is the largest.
}
}
// Takes in three double parameters.
double max(double num1, double num2, double num3)
{ // Also calculates the max but if there is three numbers to compare.
if(num1 > num2 and num1 > num3)
{
return num1; // Returns number 1 if it is greater than number 2 and 3.
}
elseif(num2 > num1 and num2 > num3)
{
return num2; // Returns number 2 if it is greater than number 1 and 3.
}
elseif(num3 > num1 and num3 > num2)
{
return num3; // Returns number 3 if it is greater than number 1 and 2.
}
else
{
cout << "Fix";
return num3;
}
}
double iceCreamShare(double iceCream, int people = 1)
{
return iceCream / people;
}
int main(int argc, char** argv) {
int choice;
do{
cout << "---------------::Main Menu::---------------\n";
cout << "Please choose one of the following options.\n";
cout << "--------------1) Max Function--------------\n";
cout << "--------------2) Ice Cream-----------------\n";
cout << "--------------3) Quit----------------------\n";
cin >> choice;
switch(choice)
{
case 1:
double num1, num2, num3;
cout << "Please enter two numbers to find the max number:";
cin >> num1 >> num2;
cout << max(num1, num2);
cout << "Please enter three numbers to find the max:";
cin >> num1 >> num2 >> num3;
cout << max(num1, num2, num3);
break;
case 2:
double iceCream;
int people;
cout << "Enter the amount of ice cream in pints: ";
cin >> iceCream;
cout << "Enter the number of people: ";
cin >> people;
cout << "Each person should get " << iceCreamShare(iceCream, people)
<< " pint(s) of ice cream.\n";
break;
}
}while(choice != 3);
return 0;
}