Sign in with
Sign up | Sign in
Your question

How To?

Last response: in Apps General Discussion
Share
March 1, 2012 8:10:10 PM

so I have two funtions how do I make them both work in the same .cpp file when I combine them kinda like run through one and another
//************************************************************************
#include <iostream>
using namespace std;


int GenerateID()
{
static int s_nID = 0;
return s_nID++;
}

int main()
{
std::cout << GenerateID() << std::endl;
std::cout << GenerateID() << std::endl;
std::cout << GenerateID() << std::endl;
return 0;
}
//************************************************************************



and
-------------------------------------------------------------------------------------------



//************************************************************************


//************************************************************************
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;


float GetTargetDistance(); // Target's Dist
float CalcWinningRange( float ); // Range to Win
float GetLauncherAngle(); // Angle of Projectile
float DegreesToRadians( float ); // Radian Conversion
float GetProjectileVelocity(); // Projectile Velocity Formula
float CalcProjectileDistance( float, float ); // Projectile Dist Calc
void PrintSummary( int, float, float, float, float); // User Output

int main()
{
float targetDistance = 0.0;
float winningRange = 0.0;
float launcherAngle = 0.0;
float ProjectileVelocity = 0.0;
float launcherRadians = 0.0;
float ProjectileDistance = 0.0;
bool hit = false;

targetDistance = GetTargetDistance();

winningRange = CalcWinningRange(targetDistance);

for(int shotNumber = 1; shotNumber <= 5; shotNumber++)
{

launcherAngle = GetLauncherAngle();

ProjectileVelocity = GetProjectileVelocity();

launcherRadians = DegreesToRadians(launcherAngle);

ProjectileDistance = CalcProjectileDistance(launcherRadians, ProjectileVelocity);

PrintSummary(shotNumber, targetDistance, launcherAngle,
ProjectileVelocity, ProjectileDistance);

if ( fabs(ProjectileDistance - targetDistance) <= winningRange )
{
hit = true;
break;
}
}

if ( hit == true )
cout << "Congrats You Win!\n";
else
cout << "To Bad Better Luck Next Time!\n";

return 1;
}

//************************************************************************
// Funstion to Get Target Dist
float GetTargetDistance()

{
float distance = 0.0;

while ( distance <= 0.0 )
{
cout << "\nTarget distance"
<< "\nIn feet: ";
cin >> distance;
}

return distance;
}

//************************************************************************
// Function to get launch Angle
float GetLauncherAngle()

{
float angle = 0.0;

while ( angle <= 0.0 || angle >= 90.0 )
{
cout << "\nThe launcher angle"
<< "\nEnter degrees: ";
cin >> angle;
}

return angle;
}

//************************************************************************
// Function to get Velocity
float GetProjectileVelocity()

{
float velocity = 0.0;

while ( velocity <= 0.0 )
{
cout << "\nThe Projectiles velocity "
<< "\nEnter Projectile velocity: ";
cin >> velocity;
}

return velocity;
}

//************************************************************************
// Function to Calc Winning Range
float CalcWinningRange( float targetDistance )

{
return (targetDistance * 0.001);
}

//************************************************************************
// Radian Converter
float DegreesToRadians( float launcherAngle )

{
return (launcherAngle * 3.14159265 / 180.0);
}

//************************************************************************
// Distance Calc
float CalcProjectileDistance( float radians, float velocity )


{
return ((velocity * velocity) * sin(2 * radians) / 32.2);
}




//************************************************************************
// Primary User Output
void PrintSummary( int shotNumber, float targetDistance, float launcherAngle,
float ProjectileVelocity, float ProjectileDistance)

{
cout << "\n***********************************************************\n"
<< "Shot Number: (" << shotNumber << ") Looks like your running out!" << endl
<< "Target Distance: " << targetDistance << endl
<< "Launcher Angle: " << launcherAngle << endl
<< "Projectile Velocity: " << ProjectileVelocity << endl
<< "Projectile Distance: " << ProjectileDistance << endl;

if ( ProjectileDistance <= targetDistance )
cout << "\nYou missed by: "
<< targetDistance - ProjectileDistance
<< " feet, Better luck next time!\n";
else
cout << "\nYou over shot by ( "
<< ProjectileDistance - targetDistance
<< " ) feet, Try Again!\n";
cout << "***********************************************************\n";
}

More about : question

March 1, 2012 10:49:19 PM

There just can't be two main() functions in the same project...
m
0
l
March 2, 2012 2:48:05 AM

try code call (it's a forum)
m
0
l
Related resources
March 8, 2012 7:07:02 PM

Ill try removing the multiple int main(), and whats code call? a forum on here or a diff website?
m
0
l
March 8, 2012 7:10:27 PM

nope removing the int mains from all the code combinations but the first one caused everything to mess up :(  I am so confused what am I missing here?
m
0
l
March 9, 2012 5:09:11 PM

hk3008 said:
nope removing the int mains from all the code combinations but the first one caused everything to mess up :(  I am so confused what am I missing here?


You need to leave just one main function. A program has to have an entry point.
m
0
l
March 17, 2012 8:43:08 PM

so woujld Imake it where there was jsut one int main() then connect them all with { and } at the begining or end, or should I float the the results together having it print a summery at the end?
m
0
l
!