# Programming assignment help!

• 03-13-2003
GrlNewB
Programming assignment help!
Hello... this is only my second time posting on these boards and yet I am asking for help again. :( I have an assignment I am working on that is supposed to be getting us used to writing functions. I am in my first C++ class so everything to this point is really basic. I am not asking for someone to write this program for me, but only point out the errors I am making with writing my functions and calling them. I keep getting errors about changing "double" type and so forth.
My assignment: in short I am supposed to use already given data to determine the total cost to produce several open-top cylindrical containers. I have to ask the user to input the dimensions and the cost per inch of the material being used, and in return output the surface are for the container and the cost of materials for the container in dollars, all formatted to 2 decimal places. I am also required to ask if they want to input another container and at the very end, display the total cost for all the cylinders entered. I am not sure how to put that into my program... not sure if I need to make some kind of loop with a counter and keep track of the sum or what. So any help there would be appreciated as well! :) My biggest problem is getting the function that does all the computations to work. Sorry to make such a long post but I am going to add in my entire program... its kinda sloppy right now... still just trying to get it to work... but I would really appreciate any help I could get with this! Thank you in advance for your time! =)

Code:

#include <iostream.h>
#include <math.h>
#include <iomanip.h>

//Function Prototype
void program ();

double surface(double, double);

//Main function

int main()
{
//Variable Declarations
int cyl_count;
double height;
double volume;
double cost;
double cyl_cost;
char another = 'Y';

//Obtain data from the user

while (another == 'Y')
{
void program ();
cout<<"Enter the radius of the base in inches: "<<endl;
cout<<"Enter the height of the container in inches: "<<endl;
cin>>height;
cout<<"Enter the cost per square inch of the material being used in dollars: "<<endl;
cin>>cost;
cout<<"\nTotal surface area for this container is: "<<setprecision(2)
<<setiosflags(ios::fixed | ios:: showpoint)<<surface<<endl;
cyl_cost = cost * surface;
cout<<"\nThe cost of the materials for this container is: "<<setprecision(2)
<<setiosflags(ios::fixed | ios:: showpoint)<<"\$"<<cyl_cost<<endl;
cout<<"\nAre there any additional containters to process? Enter Y for yes. "<<endl;
cin>>another;
cyl_count =
}

return 0;
}

//Output program description

void program()
{
cout<<"This program will determine the cost to produce open-top cylindrical containers."<<endl;

return;
}

//Computations

{
double base_area;
double surface_area;
double circumference;
double total_surface_area;
const double PI = 3.14159265;

base_area = PI * pow(radius, 2);

circumference = 2 * PI * radius;

surface_area = circumference * height;

total_surface_area = base_area + surface_area;

return surface;
}

GrlNewB :cool:
• 03-13-2003
minesweeper
Ok, from what I can make out, you are having trouble with the use of functions. Namely your surface() function.

First off the return value. I assume you want to return the base area + the surface area right? So instead of

Code:

return surface;
you do

Code:

return base_area + surface_area;
Also, when you call the function in main() you are not supplying any arguments.

You just write

Code:

surface
However your function needs 2 arguments, radius and height. So you need to give it these values when you call it as follows

Code:

Now in order to keep a running total of the cost of all your cylinders you could do with a variable named total_cost. You are correctly (well apart from the incorrect function call as explained earlier) calculating the cost of each cylinder on each loop. Once this is calculated, using:

Code:

total_cost += cyl_cost;
will make total_cost a running total of your complete cost.

Also, you have this

Code:

cyl_count =
This isn't doing anything, in fact it shouldn't compile. If you want to increment it for each cylinder. Change it to,

Code:

cyl_count++;
Hope this helps.
• 03-13-2003
DougDbug
This is an incomplete answer... I'm not at my compiler...

void program (); - When you put this inside main, you leave out the return type (void), and you place it in the sequence where you "call" it.

// Get user input
program(); // Go to program() function and do it!
// Show results

And... Your function needs a return type... you want it to return a cost or something, you don't want void in this case. And, you need parameters... something like...

// Function prototype
float program(float Area, float CostPerInch)

// Call program() function
Cost = program(Area, CostPerInch);

Remember variable "scope". The variables aren't avaliable inside your function unless they are passed-in as paramaters (arguements?) You can define local variables inside the function, PI for example. And, you could ask for user input inside the function... if that's what you wanted to do.

Start "small". Write the body of your main program first, compile and test it, then add a little and repeat. This makes it much easier to locate errors. You can also write "empty" functions to test compile.

[EDIT]
A function is (sort-of) like a detour. Go over there and do something. Then, come back here continue... maybe take some numbers with you, and maybe bring one number back. Now, just so I don't get flamed... Later, you'll learn that sometimes these "numbers" may be references or pointers. :)
• 03-13-2003
GrlNewB
Thank you...
Wow... thank you both for your relpy's! I am going to take both your suggestions and test them out to see if it fixes/helps my problems. Thanks again!
• 03-13-2003
GrlNewB
Awesome... I tried both your suggestions and was able to make it work! Only one more question... I am supposed to use a void function to output to the user the statement "This program will determine the cost to produce open-top cylindrical containers." I must be doing something wrong because I can't seem to get it to actually output the statement when I run the program. Can you see what it is I am missing?
Thanks again! :)