Overloaded Functions

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.


Put programs 1 and 2 into a menu-based operation.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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.
    }
    else if(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
line 47:
cin >> num1, num2, num3;

doesn't make sense. If you are inputting 3 ints into a stream, '>>' operator must be used 3 times.

so change it to: cin >> num1>> num2>> num3;

Last edited on
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?
Line 19: Why are you testing num2 > num1 twice? Shouldn't you be testing num3?



I meant to put num2 > num1 and num2 > num3.
I didn't spot that!
Thank you so much!!!!!!
I'm about to fix that and run my code.
*fingers crossed* haha
Between fixing my cin statement earlier and fixing my if/else if statements that was all I needed to fix my code!
It seems to work correctly now!
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <cstdlib>
#include <iostream>

using namespace 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.
    }
    else if(num2 > num1 and num2 > num3)
    {
        return num2; // Returns number 2 if it is greater than number 1 and 3.
    }
    else if(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;
}


Thank you guys so much for the help.
:]
Last edited on
Topic archived. No new replies allowed.