Making a Program

im trying to make a program to solve this equation and list the x and y variables but it is failing
here is wat i got so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cmath>
using namespace std;
void calc();

int main()
{
	calc();
}
{
	double DFx,Dfy,Vx,Vy,Ax,Ay,T;
	cout <<"What is your starting x position? ";
	cin >> Ax;
	cout <<"What is your starting y position? ";
	cin >> Ay;
	cout <<"What is your X velocity? ;" <<;
	cin >> Vx;
	cout <<"What is your Y velocity? "; <<;
	cin >> Vy;
	cout <<"What is X's acceleration? "; <<;
	cin >> Ax;
	cout <<"What is Y's acceleration? "; <<;
	cin >> Ay;
	cout <<"What is the time? ";
	cin >> T;
DFx = Vx + (Ax * (T * T));
DFy = Vy + (Ay * (T * T));
	system("pause");

	return 0;

}

help me with it plz
Check out the comments I've added to your code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int main()
{
	calc();
} // your main function ends here, was this intended?
{
        // all code below here is both outside of main and not in a function so will not compile
	
        double DFx,Dfy,Vx,Vy,Ax,Ay,T;
	cout <<"What is your starting x position? ";
	cin >> Ax;
	cout <<"What is your starting y position? ";
	cin >> Ay;
	cout <<"What is your X velocity? ;" <<;
	cin >> Vx;
	cout <<"What is your Y velocity? "; <<;
	cin >> Vy;
	cout <<"What is X's acceleration? "; <<;
	cin >> Ax;
	cout <<"What is Y's acceleration? "; <<;
	cin >> Ay;
	cout <<"What is the time? ";
	cin >> T;
DFx = Vx + (Ax * (T * T));
DFy = Vy + (Ay * (T * T));
	system("pause");

	return 0;

}


if the code below main is in fact what you wanted calc to do, you must put it into a function like this so that you can call it from main

1
2
3
4
void calc()
{
        // your code here
}


also, system("PAUSE") and return 0 should be in main not inside of your function, many people will also shout at you for using system("PAUSE") at all, but that is a separate issue.

This advice is assuming that you wanted to run that code by calling calc() but it's hard to tell as the code as it stands makes no real sense.
Also, you will need to make sure main can access the function calc() by writing its body above the main function, or by writing it below main and forward delcaring it by putting

void calc();

above main
Also do you mean to use Ax, and Ay twice?

Ax on lines 13, and 21

Ay on lines 15, and 23

EDIT: Fixed typo
Last edited on
I never noticed that but you will need separate variables for x and y's starting position and acceleration, otherwise when the user inputs the acceleration it destroys the values for the starting positions
ok

my new and improved code
take a look
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <cmath>
using namespace std;
void calc();
int main()

{
	double DFx,DFy,Vx,Vy,Ax,Ay,T,x,y;
	cout <<"What is your starting x position? ";
	cin >> x;
	cout <<"What is your starting y position? ";
	cin >> y;
	cout <<"What is your X velocity? ";
	cin >> Vx;
	cout <<"What is your Y velocity? "; 
	cin >> Vy;
	cout <<"What is X's acceleration? "; 
	cin >> Ax;
	cout <<"What is Y's acceleration? "; 
	cin >> Ay;
	cout <<"What is the time? ";
	cin >> T;
    DFx = x + (Vx + (Ax * (T * T)));
    DFy = y + (Vy + (Ay * (T * T)));
    cout <<" "<<DFx<< ", "<<DFy<<
	
	system("pause");
	return 0;
	
}

the only problem: when i open it, it goes though then the answers then immediately gone.
do i put the system pause somewhere
F.Y.I i have visual c++ 2010
Last edited on
Look like line 25 isn't properly formed. Try this:
cout <<" "<<DFx<< ", "<<DFy<<endl;

Also, make sure you have #include <cstdlib> if you are using system("pause")

Also I see that you have included cmath. That will allow you to replace (T*T) in your calculation with pow(T, 2). This is really convenient if you ever need larger exponents. More about it here:
http://www.cplusplus.com/reference/clibrary/cmath/pow/
Last edited on
That will allow you to replace (T*T) in your calculation with pow(T, 2).


In this case, I would suggest not doing that, since T*T would be a lot faster than pow(T, 2). pow() is for tricky stuff like pow(T, 6.23).
I agree, just wanted to him to be aware since he had cmath in there.
omg it works just fine
1 more question
if u wanna make it show different options for example
my answer came out as 1,1
i wanna make it so the code will show 5 different answers instead of 1
Last edited on
If I understand you right, all you need to do is add a loop. Try this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cmath>
using namespace std;
void calc();
int main()

{
	for (int i=0; i < 5; i++)
	{
	double DFx,DFy,Vx,Vy,Ax,Ay,T,x,y;
	cout <<"What is your starting x position? ";
	cin >> x;
	cout <<"What is your starting y position? ";
	cin >> y;
	cout <<"What is your X velocity? ";
	cin >> Vx;
	cout <<"What is your Y velocity? ";
	cin >> Vy;
	cout <<"What is X's acceleration? ";
	cin >> Ax;
	cout <<"What is Y's acceleration? ";
	cin >> Ay;
	cout <<"What is the time? ";
	cin >> T;
    DFx = x + (Vx + (Ax * (T * T)));
    DFy = y + (Vy + (Ay * (T * T)));
    cout <<" "<<DFx<< ", "<<DFy<<endl;
	}

	return 0;

}


Try running that code. If that's not what you meant, could you clarify what you're trying to do?
i mean like
if the answer is 1,1
the next answer is like 2,2 then 3,3 like that
closed account (D80DSL3A)
To repeat a run of the code I use this method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;
int main()
{	
	char repeat = 'n';// user queried to repeat for another case	

	do// allows repeat runs and keeps window open so results can be seen
	{
		// code to repeat

		cout << endl << "repeat (y/n)? " << flush;
		cin >> repeat;
	}while( repeat=='y');
    return 0;
}


Also, I wanted to point out that your equations of motion are incorrect. If you are treating the case of constant acceleration then they should be:
1
2
DFx = x + Vx*T + 0.5*Ax*T*T;
DFy = y + Vy*T + 0.5*Ay*T*T;
not repeat a run of a code
list more than one answer
like 5 possible answers for the equation
lets say the equation was y=3x+3
it will display all of the possible answers on y=3x+3
u know wat i mean
closed account (D80DSL3A)
No I don't know "wat" you mean. There are an infinite # of points on a line. You choose the starting value and the intervals. I don't think you know what you mean either.
Last edited on
i want to show 5 possible answers for a line
like to limit it to 5
Now do you know what i mean
5 possible answers to show man
Just throwing this out there, if you are STILL using system("pause");, or system(); anything, discontinue this.
i have visual studio so i need to use system pause
and 2 this has nothing to do with the question i was asking so ima discontinue ur comment
i want to show 5 possible answers for a line
like to limit it to 5
Topic archived. No new replies allowed.