I am having a problem with the results that I am getting when I run my program, according to the math i wrote in the program when I enter these results

object's x co-ord = 0
object's y cor-ord = 0
object's heading = 45 degrees
object's distance travelled = 1

then

object's new x,y position should display as (0.5,0.5) but everytime i run it with those values i get (0,1) and I am not sure what is wrong. All help is appreciated.

#include <iostream>
using namespace std;

int main()
{
double x1;
double y1;
int heading;
double distance;
double xHeadingRatio;
double yHeadingRatio;
    
double x2 = 0;
double y2 = 0;
    
cout << "Enter object's x co-ordinate: ";
cin >> x1;
cout << "\n";
cout << "Enter object's y co-ordinate: ";
cin >> y1;
cout << "\n";
cout << "Enter object's heading (degrees): ";
cin >> heading;
cout << "\n";
cout << "Enter object's distance travelled: ";
cin >> distance;
    
if (heading == 0)
{
y2 = y1 + distance;
}
         
if (heading == 90)
{
x2 = x1 + distance;            
}
        
if (heading == 180)
{
y2 = y1 - distance;     
}   
         
if (heading == 270)
{
x2 = x1 - distance;  
}

if (heading > 0 && heading < 90)
{
xHeadingRatio = heading / 90;
yHeadingRatio = 1 - xHeadingRatio;
x2 = distance * xHeadingRatio;
y2 = distance * yHeadingRatio;
}

if (heading > 90 && heading < 180)
{
	// math not finished
}
    
if (heading > 180 && heading < 270)
{
	// math not finished
}
    
if (heading > 270 && heading < 360)
{
	// math not finished
}

cout << "\n";
cout << "The new x and y co-ordinates are (";
cout << x2;
cout << ",";
cout << y2;
cout << ")";
cout << "\n";
system("PAUSE");
}

object's new x,y position should display as (0.5,0.5) but everytime i run it with those values i get (0,1) and I am not sure what is wrong.

Not quite. Distance of 1 from (0,0) heading 45^ will be (0.707, 0.707)

if (heading > 0 && heading < 90)
{
xHeadingRatio = heading / 90;
yHeadingRatio = 1 - xHeadingRatio;
x2 = distance * xHeadingRatio;
y2 = distance * yHeadingRatio;
}

Where did you get this formulae? Travelled distance is not proportional to heading. try

dx = distance * cos(heading / 180. * M_PI);
dy = distance * sin(heading / 180. * M_PI);

yeah i realize the math is wrong and im changing that but the program still doesnt work

Ok here is the new code, and this math works when i follow it exactly with a calculator i get (0.707, 0.707) with the previously entered values in my first post. But in the program i get like .502 and .803 something or w.e. its really weird.

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
double x1;
double y1;
int Heading;
double Distance;
    
double xDisplacement;
double yDisplacement;

double x2;
double y2;
    
cout << "Enter object's x co-ordinate: ";
cin >> x1;
cout << "\n";
cout << "Enter object's y co-ordinate: ";
cin >> y1;
cout << "\n";
cout << "Enter object's heading (degrees): ";
cin >> Heading;
cout << "\n";
cout << "Enter object's distance travelled: ";
cin >> Distance;

xDisplacement = cos(Heading) * Distance;
yDisplacement = sin(Heading) * Distance;

x2 = x1 + xDisplacement;
y2 = y1 + yDisplacement;

cout << "\n";
cout << "The new x and y co-ordinates are (";
cout << x2;
cout << ",";
cout << y2;
cout << ")";
cout << "\n";
system("PAUSE");
}

sin( ) and cos( ) take angles in radians, not degrees. Take your degree angle and multiply by (PI/180).

Note that you will find a very good value of PI in the cmath library - check what you need to access it with your compiler.

This question has already been answered. Start a new discussion instead.