-
First Program Issues
Hey Folks,
I'm writing my first program outside of my intro c++ class...its supposed to take initial velocity and angle for a projectile and calculate its horizontal and vertical range. I've put the projectile into a class with variables initial velocity and initial angle. I've also tried to include and use trigonometry for the first time. My problem is that the user input of the initial velocity and angle is not replacing my initialized values for the member variables. If I can post the code correctly, I would appreciate some help...
Code:
/* Title: Projectiles -- Two Dimensional Projectile Program
Author: Sam
Date: July 27, 2009 */
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Projectile {
double InitialVelocity;
double InitialAngle;
public:
//constructors...
Projectile () { ; } // default constructor
Projectile (double v0, double Angle) { //initialzing constructor
InitialVelocity = v0;
InitialAngle = Angle;
}
//mutators...
void SetInitialVelocity(double v0) { InitialVelocity = v0; }
void SetInitialAngle(double Angle) { InitialAngle = Angle; }
//accessors...
double GetInitialVelocity() { return InitialVelocity; }
double GetInitialAngle() { return InitialAngle; }
};
void Menu();
double Collect(Projectile);
void HRange(Projectile);
int main() {
Projectile Alpha;
string Command;
while (true) {
Menu();
cout << "Command: ";
getline (cin, Command);
if (Command == "Quit")
break;
else if (Command == "Enter Initial Values")
Collect(Alpha);
else if (Command == "Calculate Horizontal Range")
HRange(Alpha);
}
}
void Menu() {
cout << "\n============Menu============" << endl;
cout << "Choices: " << endl;
cout << "\t Enter Initial Values" << endl;
cout << "\t Calculate Horizontal Range" << endl;
cout << "\t Calculate Vertical Range (Height)" << endl;
cout << "\t Quit" << endl;
cout << "============================" << endl;
}
double Collect(Projectile Alpha) {
int v0, Angle;
cout << "Enter Initial Velocity: ";
cin >> v0;
cout << "Enter Initial Angle: ";
cin >> Angle;
cout << "Initial Velocity = " << v0 << " m/s" << endl;
cout << "Initial Angle = " << Angle << " degrees" << endl;
Alpha.SetInitialAngle(Angle);
Alpha.SetInitialVelocity(v0);
cin.ignore (100, '\n');
return 0;
}
void HRange(Projectile Alpha) {
double g = 10;
double XRange;
double Pi = 3.1416;
double CosRadianAngle;
double SinRadianAngle;
cout << Alpha.GetInitialAngle();
double RadianAngle = Alpha.GetInitialAngle() * Pi / 180;
SinRadianAngle = sin (RadianAngle);
CosRadianAngle = cos (RadianAngle);
cout << "Sine of angle is: " << SinRadianAngle << endl;
cout << "Cosine of angle is: " << CosRadianAngle << endl;
XRange = (2*(Alpha.GetInitialVelocity() * Alpha.GetInitialVelocity() * CosRadianAngle * SinRadianAngle) / g);
cout << "Horizontal Range = " << XRange << endl;
}
-
You're passing your Alpha variable by value into the function. This creates a copy and the copy is changed inside the function. The variable in main is not changed, though.
You need to use pass-by-reference. This simplest way to do that is to use references:
Code:
double Collect(Projectile& Alpha)
Make sure to change both the function prototype at the top and the definition later.
Note that HRange doesn't need to change, because it doesn't modify the passed in variable.
-
Thanks buddy...I don't quite understand that, but it did fix it, and I'll read up about references and pointers, n such.
-
Another option here, which might even be better, is to make the Collect function a member of Projectile.
It's not the best idea to do input from cin directly inside a class's member function, but at this stage of your learning I think it's ok.
If you make Collect (and HRange) a member function, then you don't need to pass the Projectile to the function at all, so you won't need pass-by-reference. Instead you'd save the values directly in the member variables. I'd suggest trying that to see if you can get it working.
-
HRange really should be a member function, imo.
Code:
void Projectile::HRange(void) {
double g = 10;
double XRange;
double Pi = 3.1416;
double CosRadianAngle;
double SinRadianAngle;
cout << GetInitialAngle();
double RadianAngle = GetInitialAngle() * Pi / 180;
SinRadianAngle = sin (RadianAngle);
CosRadianAngle = cos (RadianAngle);
cout << "Sine of angle is: " << SinRadianAngle << endl;
cout << "Cosine of angle is: " << CosRadianAngle << endl;
XRange = (2*(GetInitialVelocity() * GetInitialVelocity() * CosRadianAngle * SinRadianAngle) / g);
cout << "Horizontal Range = " << XRange << endl;
}
Or if you wanted to get all fancy and whatnot you could do this....
Code:
std::ostream &operator<<(std::ostream &out, Projectile &Alpha)
{
double g = 10;
double XRange;
double Pi = 3.1416;
double CosRadianAngle;
double SinRadianAngle;
out << Alpha.GetInitialAngle();
double RadianAngle = Alpha.GetInitialAngle() * Pi / 180;
SinRadianAngle = sin (RadianAngle);
CosRadianAngle = cos (RadianAngle);
out << "Sine of angle is: " << SinRadianAngle << std::endl;
out << "Cosine of angle is: " << CosRadianAngle << std::endl;
XRange = (2*(Alpha.GetInitialVelocity() * Alpha.GetInitialVelocity() * Alpha.CosRadianAngle * SinRadianAngle) / g);
out << "Horizontal Range = " << XRange << std::endl;
return out;
}
Which would allow you to do stuff like cout << myProjectile << endl; Or use filestreams too.