# Thread: C++ using function, nested decisions and sentinel loops question

1. ## C++ using function, nested decisions and sentinel loops question

I thought I finished my homework/project and I'm assuming its a mess. It makes sense to me, but can someone help me out? I'm suppose to input A, X, V or Q (Area, cross section, area surface). Then a radius and height. The output is suppose to be two output arguments. a float representing thr radius and float for the height. Below is my coding. I'd really appreciate the help.

Code:

Code:
```#include <iostream>
#include <cmath>
using namespace std; //intriduces namespace std

const float PI = 3.14159f;
void getdata (float &);
float volume (float);
float surface_area (float);
float cross_section (float);
float circum (float);
float height (float);
void putdata (float, float, float);//writes the radius plus two resuts

int main (void) {
float radius, c, h, v, a, x;
system ("pause");
return 0;
}

{
cout<<cout <<"Enter character V to find volume of a sphere, A to find the area, and X to find the Cross Section Area. Follow character input with radius and then wither height or circumference. End program by inputting Q.\n"<<endl;

cin >> input;
}
float volume (float r & h)
{return PI*h*r*r;
}

float surface_area (float r & c)
{return (PI*r*r*2) + (c*h);
}

float cross_section (float r)
{return PI*r*r;
}

void putdata (float radius, float c, float h, float v, float a, float x)

cout<<"Circumference ="<<c<<endl;

cout<<"Volume ="<<v<<endl;

cout<<"Surface Area ="<<a<<endl;

cout<<"Cross Section ="<<x<<endl;
}```

2. O_o

I didn't really get a question in all that.

What "help" do you require?

Soma

3. 1. These function definitions
Code:
```float volume (float r & h)
float surface_area (float r & c)```
are invalid and don't match the prototypes:
Code:
```float volume (float);
float surface_area (float);```
2. You need to include an appropriate header file to call system function.

4. Line 32, two cout's

getdata is not returning anything. Functions do not work like that. I think you would be interested in using a class.

Code:
`float surface_area (float r & c)`

I think you are trying to pass by reference but only for one variable? You are also missing a comma....

You really need to write a plan for each function before programming. You are going to save yourself a lot of time. Take the advice or learn the hard way, either way keeping coding

5. Originally Posted by jocdrew21
getdata is not returning anything. Functions do not work like that. I think you would be interested in using a class.
Nonsense. A function can return something or it can not. Nothing is wrong with either approach.
Substituting with a class when you are just going to call one member function and which does not even contain any member data is pointless.

Onwards to errors...
Code:
```const float PI = 3.14159f;
void getdata(float &);
float volume(float);
float surface_area(float);
float cross_section(float);
float circum(float);
float height(float);
void putdata(float, float, float);//writes the radius plus two resuts```
Don't remove the parameter names from the prototype declarations. This is considered bad practice.
They also don't match the definitions. That's an error.

Code:
```void getdata(float & radius)
{
cout << cout << "Enter character V to find volume of a sphere, A to find the area, and X to find the Cross Section Area. Follow character input with radius and then wither height or circumference. End program by inputting Q.\n" << endl;
cin >> input;
}```
Don't output twice to cout. It should only be once.
radius parameter is unused and input is undeclared. You should either return the input value or read into the correct parameter (e.g. radius).

Code:
```float volume(float r & h)
{
return PI*h*r*r;
}```
Syntax error in function definition. "float r & h" is not valid syntax. Did you mean to say that the function accepts two arguments? If so, you mean to write "float r, float h".

Code:
```float surface_area(float r & c)
{
return (PI*r*r * 2) + (c*h);
}```
Same syntax error as before. h is undeclared. Did you forget to pass it as an argument?

Code:
`void putdata(float radius, float c, float h, float v, float a, float x)`
Misleading name. Probably should be something along the lines of "printdata".

Last, but not least, format and indent your code properly:
Code:
```#include <iostream>
#include <cmath>
using namespace std; //intriduces namespace std

const float PI = 3.14159f;
void getdata(float &);
float volume(float);
float surface_area(float);
float cross_section(float);
float circum(float);
float height(float);
void putdata(float, float, float);//writes the radius plus two resuts

int main(void)
{
float radius, c, h, v, a, x;
system("pause");
return 0;
}

{
cout << cout << "Enter character V to find volume of a sphere, A to find the area, and X to find the Cross Section Area. Follow character input with radius and then wither height or circumference. End program by inputting Q.\n" << endl;
cin >> input;
}

float volume(float r & h)
{
return PI*h*r*r;
}

float surface_area(float r & c)
{
return (PI*r*r * 2) + (c*h);
}

float cross_section(float r)
{
return PI*r*r;
}

void putdata(float radius, float c, float h, float v, float a, float x)
{
cout << "Circumference =" << c << endl;
cout << "Volume =" << v << endl;
cout << "Surface Area =" << a << endl;
cout << "Cross Section =" << x << endl;
}```

6. Functions do not work like that. I think you would be interested in using a class.
O_o

The reference parameter indicates that the function writes the result to the argument, but the implementation is still flawed.

Soma

7. @Elysia, I think you misunderstood me. I said he was not returning anything in getdata yet asking for input in the function. Therefore the function is not really doing anything.

8. The function has an in parameter, named radius. It could be used to get input back to the caller. But as it stands right now, the function isn't doing anything, no.