Making a Program

Nov 30, 2010 at 1:17am
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
Nov 30, 2010 at 1:35am
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.
Nov 30, 2010 at 1:40am
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
Nov 30, 2010 at 1:40am
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 Nov 30, 2010 at 1:41am
Nov 30, 2010 at 1:43am
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
Nov 30, 2010 at 1:52am
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 Nov 30, 2010 at 2:21am
Nov 30, 2010 at 2:51am
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 Nov 30, 2010 at 2:52am
Nov 30, 2010 at 2:54am
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).
Nov 30, 2010 at 2:59am
I agree, just wanted to him to be aware since he had cmath in there.
Nov 30, 2010 at 3:11am
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 Nov 30, 2010 at 3:22am
Nov 30, 2010 at 3:30am
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?
Nov 30, 2010 at 3:37am
i mean like
if the answer is 1,1
the next answer is like 2,2 then 3,3 like that
Nov 30, 2010 at 4:16am
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;
Nov 30, 2010 at 4:44am
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
Nov 30, 2010 at 1:05pm
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 Nov 30, 2010 at 1:14pm
Nov 30, 2010 at 8:29pm
i want to show 5 possible answers for a line
like to limit it to 5
Now do you know what i mean
Dec 1, 2010 at 8:01pm
5 possible answers to show man
Dec 1, 2010 at 8:04pm
Just throwing this out there, if you are STILL using system("pause");, or system(); anything, discontinue this.
Dec 2, 2010 at 1:00am
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
Dec 2, 2010 at 3:47am
i want to show 5 possible answers for a line
like to limit it to 5
Topic archived. No new replies allowed.