Shape Volume/Weight decider using functions

I have written this program which is testing the use of functions and aims to take in 3 inputs weight, length and diameter of a shape and validates them. Next an estimated volume is calculated by doing pie * (radius squared) * length. From this a weight is calculated using a conversion of 1mm cubed to 0.05 grams. Finally this weight is to be compared against the weight entered at the start and the shape is determined as either a wedge or a cylinder depending on whether the difference in the weights is above 0.1 grams.

However I cant get any output as errors like "6:39: error: expected identifier before numeric constant
6:39: error: expected ',' or '...' before numeric constant" are coming up.

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
 #include <iostream>
using namespace std;

bool isValidDimensions(double length, double diameter);
double eval_volume(double length, double diameter);
double eval_weight(double est_volume, 0.05);
void wedge_or_cylinder(double est_weight, double act_weight);

int main()
{
double length = 0; double diameter = 0; double act_weight = 0;
double est_volume = 0; double est_weight = 0;

cout << "Enter the length:"<< endl;
cin>> length;

cout << "Enter the diameter" << endl;
cin >> diameter;

cout << "Enter the actual weight:" << endl;
cin >> act_weight;

if (!isValidDimensions(length, diameter))
// check len is within the range 1mm to 20mm and dia is in range 0.1mm - 1mm
{
    cout<<"Dimensions are not valid";
}

else
{
// estimate volume
est_volume = eval_volume(length, diameter);
// estimate weight, density 0.05
est_weight = eval_weight(est_volume, 0.05);
// display if wedge or cylinder
wedge_or_cylinder(est_weight, act_weight);

cout <<"Volume is " <<est_volume << endl;
cout <<"Weight is " <<est_weight << endl;
cout <<"The shape is a " <<wedge_or_cylinder;
}

return 0;
}

bool isValidDimensions(double length, double diameter)
{
    if (length <1.00 || length >20.00)
    {
        return false;
    }
    else
    {
        return true;
    }
    
     if (diameter <0.1 || diameter >1.00)
    {
        return false;
    }
    else
    {
        return true;
    }
   
}

double eval_volume(double length, double diameter)
{
    double radius = diameter/2;
    double radius_squared = radius*radius;
    double est_volume = 3.142 * radius_squared * length;
   
    return est_volume;
}

double eval_weight(double est_volume, 0.05)
{
    double est_weight = est_volume/0.05;
    return est_weight;
}

void wedge_or_cylinder(double est_weight, double act_weight)
{
    if (est_weight > act_weight + 0.10 || est_weight < act_weight - 0.10)
    {
        cout <<"wedge";
    }
    else
    {
        cout <<"cylinder";
    }
}
that says that line 6 is messed up.

1
2
3
4
6:39: error: expected identifier before numeric constant
^ line number
    ^^ line position
          ^^error complaint:  found an unexpected constant value here

and it is

double eval_weight(double est_volume, 0.05);

you can't do that.
you can do this:
double eval_weight(double est_volume); //no value needed, pass 0.05 if that is what you want when you call it?!
or this:
double eval_weight(double est_volume = 0.05); // gives est_volume a default value.

or this:
double eval_weight(double est_volume, double whatisthis = 0.05); //was it a second parameter you wanted?

but you can't just have a number in a function parameter list.

Last edited on
Okay thanks for that, Iv made a couple of changes and it almost completely works. I get all output except either "wedge" or "cylinder"?

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
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
using namespace std;

bool isValidDimensions(double length, double diameter);
double eval_volume(double length, double diameter);
double eval_weight(double est_volume);
string wedge_or_cylinder(double est_weight, double act_weight, string shape);

int main()
{
double length = 0; double diameter = 0; double act_weight = 0; double est_volume = 0; double est_weight = 0; string shape;

cout << "Enter the length:"<< endl;
cin>> length;

cout << "Enter the diameter" << endl;
cin >> diameter;

cout << "Enter the actual weight:" << endl;
cin >> act_weight;

if (!isValidDimensions(length, diameter))
// check len is within the range 1mm to 20mm and dia is in range 0.1mm - 1mm
{
    cout<<"Dimensions are not valid";
}

else
{
// estimate volume
est_volume = eval_volume(length, diameter);
// estimate weight, density 0.05
est_weight = eval_weight(est_volume);
// display if wedge or cylinder
wedge_or_cylinder(est_weight, act_weight, shape);

cout <<"Volume is " <<est_volume << " mm" << endl;
cout <<"Weight is " <<est_weight << " g" << endl;
cout <<"The shape is a " <<shape;
}

return 0;
}

bool isValidDimensions(double length, double diameter)
{
    if (length <1.00 || length >20.00)
    {
        return false;
    }
    else if (diameter <0.1 || diameter >1.00)
    {
        return false;
    }
    else
    {
        return true;
    }
   
}

double eval_volume(double length, double diameter)
{
    double radius = diameter/2;
    double radius_squared = radius*radius;
    double est_volume = 3.142 * radius_squared * length;
   
    return est_volume;
}

double eval_weight(double est_volume)
{
    double est_weight = est_volume/0.05;
    return est_weight;
}

string wedge_or_cylinder(double est_weight, double act_weight, string shape)
{
    
    if (est_weight > act_weight + 0.10 || est_weight < act_weight - 0.10)
    {
        shape = "wedge";
    }
    else
    {
        shape = "cylinder";
    }
    return shape;
}


OUTPUT:
Enter the length:
15
Enter the diameter
1
Enter the actual weight:
20
Volume is 11.7825 mm
Weight is 235.65 g
The shape is a
Last edited on
20
Volume is 11.7825 mm
Weight is 235.65 g

that weight is more than 11 times larger than the actual. Computation problem?

maybe get rid of shape, it is not necessary or useful.

1
2
3
4
5
 if (est_weight > act_weight + 0.10 || est_weight < act_weight - 0.10)
    {
        return "wedge";
    }
       return "cylinder";


and in main, make a choice...
shape = wedge_or_cylinder(est_weight, act_weight, shape); //do this
OR DO THIS

cout <<"Volume is " <<est_volume << " mm" << endl;
cout <<"Weight is " <<est_weight << " g" << endl;
cout <<"The shape is a " << wedge_or_cylinder(est_weight, act_weight, shape);

OR make it passed by reference and a void function (no return statements).

shape is not modified by the function because it isnt passed by reference.
you do not use the returned value.
Last edited on
Ok got it working now, thanks man
Topic archived. No new replies allowed.