Trouble passing values to function.

Hey what's up, pretty simple question I think, i'll post my code after I post my error.

Error :
12 :too few arguments to function `double mult(double, double, double, double)'

32 :at this point in file

I can't figure out how to remove this error, can someone lend me a hand??


Thanks for your time!


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
/*Program i made to multiply two numbers; then the user decides if they should
round the values up or down. based on the user input, if the string is equal
to up, 1 is assigned to the value round, else if the string is equal to down, 0
is assigned to the value round. If the user enters something other than up or down
no rounding is assumed. These values are then passed through the function 
mult for an answer.*/

#include <iostream>
#include <cmath>

using namespace std;

double mult(double a, double b, double c, double r);  // LINE 12

int main()
{
    cout << "First number: " << endl;
    double fnum;
    cin >> fnum;
    cout << "Second number: " << endl;
    double snum;
    cin >> snum;
    cout << "Would you like to round up or down the answer?";
    string rounding;
    cin >> rounding;
    double round;
    if (rounding.compare("up"))
    round = 1;
    else if(rounding.compare("down"))
    round = 0;
    else
    round = 2;
    cout << mult(fnum, snum, round); // LINE 33
    

    
    
    cin.get();
    cin.get();
    return 0;
}

double mult(double a, double b, double c, double r)
{
    if (c == 0)
    {
       r = floor(a * b);
    }
    else if (c == 1)
    {
       r = ceil(a*b);
    }
    else
    {
        r = a*b;
    }
    return r;
}
    
Your function asks for 4 numbers but you only pass in 3, which is too few.
When you use mult(), it expects you to use all 4 arguments. At line 33 you only use 3, and it is expecting one more.
I thought it was possible to not pass a value assuming i declare the value in the function header.

When I had line 43 set to "double mult(double a, double b, double c, double r = 2)

it still wouldn't work..

I guess i'm asking if its possible to pass less values to the functions or pass a NULL value to a function(since really i only want 3, the 4th is just the return value);

or could I just write the function mult as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
double mult(double a, double b, double c)
{
    if (c == 0)
    {
       return = floor(a * b);
    }
    else if (c == 1)
    {
       return = ceil(a*b);
    }
    else
    {
        return = a*b;
}

That's true, the code you posted doesn't give a default value to double r. Maybe you left it out when you were compiling it.
it's quite possible that I didn't, i was scrambling between variations of the code trying to get my brain into C++ mode (usually start off by writing something random based off the newest stuff I learned the last time i programmed).

Thanks for helping by the way, truly appreciate it.

This is an awesome forum!!
Topic archived. No new replies allowed.