Function Prototype

Hi, I'm a computer dummy taking my first (and likely only) computer science course and I've begun to really struggle. I thought my current project was going well until I actually want to run the program and everything came out wrong. I was hoping that somebody would be able to point out where I made a mistake.

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

double drag(double p, double vel, double cd, double area);

// Main Program
int main( )
{
// Constant Declarations
const double PI = 3.1415926535; // the radius/diameter of a circle
cout << fixed << setprecision(2);

// Variable Declarations
double p; // air density (entered by user; in kg/m^3)
double vel; // velocity of the rocket (entered; in m/s)
double cd; // drag coefficient of rocket (entered)
double diameter; // diameter of rocket (entered by user; in mm)
double dia2; // diameter of rocket (calculated; in m)
double area; // frontal area of rocket (calculated; in m^2)
double AeDrag; // aerodynamic drag acting on rocket (calculated;in n)
char ans; // y/n if user wants to run program again


do {cout << "enter air density at launch site (kg/m^3): ";
cin >> p;
cout << "enter velocity of the rocket (m/s): ";
cin >> vel;
cout << "enter coefficient of drag: ";
cin >> cd;
cout << "enter diameter of the rocket (mm): ";
cin >> diameter;
dia2=diameter/1000;
area= PI*(dia2/2)*(dia2/2);
AeDrag=drag(p, vel, cd, area);
cout << " The drag on the rocket is " << drag << " newtons. " << endl;
cout << "Would you like to run the program again (Y or N)?" << endl;
cin >> ans;
} while (ans =='Y' || ans == 'y');



return 0;
}

double drag(double p, double vel, double cd, double area)
{
double ans = double (.5*p*vel*vel*cd*area);
return ans;
}
cout << " The drag on the rocket is " << drag << " newtons. " << endl;
'drag' is a function. When you called that function, you stored the return value into AeDrag. Print that.
Thanks a lot, that was a mistake that I should have caught. The final step is to add an exit function for if the velocity exceeds 300 m/s. How exactly do I implement the exit function?
Returning from main() causes the program to end. From what I see there, the program is already capable of finishing.

@28 cout << "enter velocity of the rocket (m/s): ";
@29 cin >> vel;

Add the following code if you want to exit on velocity exceeding 300 m/s

@30 if(vel > 300)
@31 exit(0);

Argument to exit function can be any integer value (0,1,-1 etc).
Topic archived. No new replies allowed.