Function assignment problems

I have an assignment where I'm supposed to take an old code and rewrite it using functions. I'm still a little shady on where I'm supposed to include certain parts of information. Could someone take a look at them and see where I misplaced the information? I'm brand new to this so please don't assume this is sloppy work because I'm lazy. This is my original code:

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
  #include <iostream>
#include <cmath>
#include <iomanip>


using namespace std;

int main()
{
   // Declare and initialize variables and constants

   //(r = radius, g = acceleration due to gravity,
   // ds = solid density, df = fluid density,
   // fv = fluid viscosity, v = velocity, t = time,
   // d = distance to bottom of tank.)

   double r, g, d, ds, df, fv, v, t, df1, ds1, fv1;

   //**Print values**//

   //Constants

   cout << "Distance to bottom of tank =  20.0 cm" << endl;
   cout << "Radius of the particle =       0.5 cm" << endl;
   cout << "Local value of g (gravity) = 980.00 cm/s^2" << endl;

   //User inputs

   cout << endl;
   cout << "Please enter a value for the fluid density (g/cm^3): ";
   cin >> df;

   cout << "The value you entered is " << df << endl;
   df1=df;

   cout << endl;
   cout << "Please enter a value for the solid density (g/cm^3): ";
   cin >> ds;
   cout << "The value you entered is " << ds << endl;
   ds1=ds;

   cout << endl;
   cout << "Please enter a value for the fluid viscosity (g/cm-s): ";
   cin >> fv;
   cout << "The value you entered is " << fv << endl;
   fv1=fv;

   r=0.5, g=981, d=20;
   v = ((2.0/9)*((ds1-df1)/fv1)*g*r*r)/100;
   t = (d/v);

   //Calculations

   cout << endl;
   cout << fixed << setprecision(2);
   cout << "Fluid density: " << setw (10) << df << " g/cm^3" << endl;
   cout << "Solid density: " << setw (10) << ds << " g/cm^3" << endl;
   cout << "Fluid viscosity: " << setw (8) << fv << " g/cm-s" << endl;
   cout << "Velocity: " << setw(15) << v << " cm/s" << endl;
   cout << "Falling time: " << setw (11) << t << " s" << endl;

   return 0;
}


and this is the one I'm trying to create using functions:

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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

//Constants

void printInformation ()
{
   cout << "Distance to bottom of tank =  20.0 cm" << endl;
   cout << "Radius of the particle =       0.5 cm" << endl;
   cout << "Local value of g (gravity) = 980.00 cm/s^2" << endl;
}

// Declare and initialize variables and constants

   //(r = radius, g = acceleration due to gravity,
   // ds = solid density, df = fluid density,
   // fv = fluid viscosity, v = velocity, t = time,
   // d = distance to bottom of tank.)

    void calculateVelocityAndTime(double r, double g, double d, double df,
        double ds, double fv, double& v, double& t)
 {  v = ((2.0/9)*((ds-df)/fv)*g*r*r)/100;
    t = (d/v);
   cout << endl;
   cout << fixed << setprecision(2);
   cout << "Fluid density: " << setw (10) << df << " g/cm^3" << endl;
   cout << "Solid density: " << setw (10) << ds << " g/cm^3" << endl;
   cout << "Fluid viscosity: " << setw (8) << fv << " g/cm-s" << endl;
   cout << "Velocity: " << setw(15) << v << " cm/s" << endl;
   cout << "Falling time: " << setw (11) << t << " s" << endl;
 }

    void displayResults(double dp, double mu, double r,
        double H, double Ho, double velocity, double time);

int main()
{
    printInformation();
    cin << df << endl;
    cin << ds << endl;
    cin << fv << endl;
    displayesults();
    cout << v << " " << t << endl;
   return 0;
}
I suggest making a double returning fuction for each variable that need a calculation. for example:
1
2
3
4
double getVelocity( double solidDensity, double fluidDensity, double gravity, double radius)
{
    //formula for calculating velocity
}
Does this look right?

1
2
3
4
5
6
7
8

    void velocity ( double sd, double sd, double g, double r);

    void time (double d, double v);

    void calculateVelocityAndTime(double r, double g, double d, double df,
        double ds, double fv, double& v, double& t)
make them double returning

double velocity(/*stuff*/)
double time(/*stuff*/)

etc...
@Kyle

The OP has the function calculateVelocityAndTime with references, so it is fine being void.

@OP

Having said that it might be good practice for the you to have 2 functions that return a const reference to double, which is either assigned to something or used in a std::cout statement.

Hope all goes well.
I rewrote it and tried cleaning it up. Can someone explain why I keep getting an error message saying why the variables in the display results in main were not declared?

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

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

void printInformation ();

void askQuestions(double df, double ds, double fv);

void displayResults(double df, double ds, double fv, double v,
 double t);

int main()
{
    float df, ds, fv;

    printInformation();
    askQuestions (df, ds, fv);
    displayResults(df, ds, fv, v, t);

   return 0;
}

//Constants display

void printInformation ()
{
   cout << "Distance to bottom of tank =  20.0 cm" << endl;
   cout << "Radius of the particle =       0.5 cm" << endl;
   cout << "Local value of g (gravity) = 980.00 cm/s^2" << endl;
   cout << endl;
}

void askQuestions (float fd, float ds, float fv)
{
   cout << "Please enter a value for fluid density " << endl;
   cin >> df ;
   cout << "Please enter a value for solid density " << endl;
   cin >> ds;
   cout << "Please enter a value for fluid viscosity " << endl;
   cin >> fv ;

   {void displayResults(double df, double ds, double fv, double v,
 double t)
{
   cout << endl;
   cout << fixed << setprecision(2);
   cout << "Fluid density: " << setw (10) << df << " g/cm^3" << endl;
   cout << "Solid density: " << setw (10) << ds << " g/cm^3" << endl;
   cout << "Fluid viscosity: " << setw (8) << fv << " g/cm-s" << endl;
   cout << "Velocity: " << setw(15) << v << " cm/s" << endl;
   cout << "Falling time: " << setw (11) << t << " s" << endl;
}

   }

}
double addNumbers (double df, double ds, double fv, double g, double r, double d, double v, double t)
{
   r=0.5, g=981, d=20;
   v = ((2.0/9)*((ds-df)/fv)*g*r*r)/100;
   t = (d/v);
}

Last edited on
Hi allensark,

It's just the message says - variables v and t were not declared in main(). Add them on line 18. But you should declare 1 variable per line, initialise it to something, and put a comment if necessary. The comment can be about the expected range of the value - for example must be positive say.

When you have compiler errors, you should post them here in full.

Line 18: prefer to use double rather than float

Line 37: all these parameters need to be references, so their values are available in main()

Line 45: put a closing brace for the askQuestions function

Line 46: remove the opening brace

addNumbers function: You haven't declared this before main, and I don't see where you have called it from. If you are not going to use references, then this should be 2 functions, because you need to return 2 variables. You shouldn't need to set values on line 63, because they are set in the arguments when you call the function. To return a value, you need a return statement. There should have been a warning about that

This function was fine in your original code, I was just suggesting to have 2 functions returning values as good practice - I still think this is a good idea.

I think you need to understand more about scope of variables:

Variables declared inside any function (including main())are only available inside that function unless they are sent to another function via arguments in a function call. Variables that are sent this way do not affect value of variables in the function they were called from, unless they were sent as references or pointers in which case the function can optionally be of type void.

A variable (one) can be sent back to the calling function via any function with a return type that is not void. There needs to be a return statement, and the value returned needs to be assigned or used in a std::cout statement say.

Hope this helps a bit :+)
Thank you so much TheIdeasMan! You really have helped me understand this more. I apologize for not replying a lot sooner to your message. I'm cleaned up my code a bit more bit found it hard to understand two comments you made. The first being line 37 where the parameters needed to be references. Does that mean I should move them within the main()? My second is the return statement. I typed up two because I was not sure which one was correct, if any. Would you mind taking a look at my code one more 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

void printInformation ();

void askQuestions(double df, double ds, double fv);

void displayResults(double df, double ds, double fv);

int main()
{
    double df, ds, fv;

    printInformation();
    askQuestions (df, ds, fv);
    displayResults(df, ds, fv);

    void askQuestions (float df, float ds, float fv);
{
   cout << "Please enter a value for fluid density " << endl;
   cin >> df ;
   cout << "Please enter a value for solid density " << endl;
   cin >> ds;
   cout << "Please enter a value for fluid viscosity " << endl;
   cin >> fv ;
}

   return 0;
}

//Constants display

void printInformation ()
{
   cout << "Distance to bottom of tank =  20.0 cm" << endl;
   cout << "Radius of the particle =       0.5 cm" << endl;
   cout << "Local value of g (gravity) = 980.00 cm/s^2" << endl;
   cout << endl;
}
 void displayResults(double df, double ds, double fv, double v,
 double t);
{
   cout << endl;
   cout << fixed << setprecision(2);
   cout << "Fluid density: " << setw (10) << df << " g/cm^3" << endl;
   cout << "Solid density: " << setw (10) << ds << " g/cm^3" << endl;
   cout << "Fluid viscosity: " << setw (8) << fv << " g/cm-s" << endl;
   cout << "Velocity: " << setw(15) << v << " cm/s" << endl;
   cout << "Falling time: " << setw (11) << t << " s" << endl;
   }

}

double addNumbers (double df, double ds, double fv, double g, double r, double d, double v, double t)
{
   r=0.5, g=981, d=20;
   v = ((2.0/9)*((ds-df)/fv)*g*r*r)/100;
   t = (d/v);
   return v
}

double addNumbers (double df, double ds, double fv, double g, double r, double d, double v, double t)
{
   r=0.5, g=981, d=20;
   v = ((2.0/9)*((ds-df)/fv)*g*r*r)/100;
   t = (d/v);
   return t
}


EDIT: Here are the error messages I keep getting:

line 53 error: expected unqualified-id before '{' token

line 63 error: expected declaration before '}' token

||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Last edited on
The functions should be defined outside of the main function.

When you actually define a function (as opposed to the function prototype declaration at the beginning), don't put a semicolon after the parameters.
void askQuestions (float df, float ds, float fv);
(think you meant double, not float there)
void displayResults(double df, double ds, double fv, double v,double t);


A semi-colon is missing after these return statements.
return v return t
Last edited on
closed account (iAk3T05o)
What are you doing?
1
2
3
4
5
int main ()
{
double fv;
calculateblah (float fv); //it should be calculateblah (fv)
}

so what's the usefulness of double fv or float fv?
Topic archived. No new replies allowed.