Thread: First Program Issues

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    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;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    2
    Thanks buddy...I don't quite understand that, but it did fix it, and I'll read up about references and pointers, n such.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  5. #5
    Registered User Cooloorful's Avatar
    Join Date
    Feb 2009
    Posts
    59
    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.
    wipe on -
    A slap on the hand is better than a slap on the face. A tragic lesson learned far too late in life.
    - wipe off

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Continued issues with Winows Errors when running program
    By hpteenagewizkid in forum C Programming
    Replies: 6
    Last Post: 11-14-2006, 03:51 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  5. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM