Creating Velocity program

I am trying to create a program that calculates velocity and acceleration, the following information is given:
After each of the position measurements has been entered, calculate and display the following:
1. the average velocity for this time interval
2. the average acceleration for this time interval
Calculations
Use the following formulas for your calculations:
change of position = current position - previous position
change of velocity = current velocity - previous velocity
average velocity = change of position / time interval
average acceleration = change of velocity / time interval

I am having trouble with the average velocity and acceleration now showing the second time around the loop. I have a sample output too.
This is the code I have created 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
33
34
35
36
37
38
39
40
41
42
43
44
                                                				
#include <iostream>
#include <iomanip>                              				

using namespace std;                           					

int main()                                      				
{                              
	int choice,
	count=0;
	cout << "Motion Analysis!";
	double position = 0,
		   timeInt,
		   chngPos,
		   prevPos,
		   prevVel,
		   chngVel,
		   avgVel,             
		   avgAccel,
		   crntVel = 0;
	
	cout << fixed <<setprecision(2);
	
	cout << "\nEnter the uniform time interval in seconds: ";
	cin >> timeInt;
	    	     	             
	while (1)
	{ 
		cout << "\nEnter a position in feet (negative value to quit): ";
		cin >> prevPos; 
		  	 
			if  (prevPos <= 0)
				break;
									
		   else	avgVel = prevPos / timeInt;
				avgAccel = avgVel / timeInt;
				 	  
				cout << "\nAverage velocity (this interval): "<<avgVel<<" feet/second"; 
				cout <<"\nAverage acceleration: " <<avgAccel<<" feet/second";
							
				 
	}
	return (0);
}

I am really new with C++ and I am having a little bit of a hard time.


This is the sample output:
Motion Analysis

Enter the uniform time interval in seconds: 2.5

Enter a position in feet (negative value to quit): 2

Average velocity (this interval): 0.80 feet/second
Average acceleration: 0.32 feet/second/second

Enter a position in feet (negative value to quit): 5

Average velocity (this interval): 1.20 feet/second
Average acceleration: 0.16 feet/second/second

Enter a position in feet (negative value to quit): 4

Invalid value: 4 is less than the current position of 5. Try again.

Enter a position in feet (negative value to quit): 12

Average velocity (this interval): 2.80 feet/second
Average acceleration: 0.64 feet/second/second

Enter a position in feet (negative value to quit): -1





I am getting this as my output

Motion Analysis

Enter the uniform time interval in seconds: 2.5

Enter a position in feet (negative value to quit): 2

Average velocity (this interval): 0.80 feet/second
Average acceleration: 0.32 feet/second/second
Enter a position in feet (negative value to quit): 5

Average velocity (this interval): 2.00 feet/second
Average acceleration: 0.80 feet/second/second
Enter a position in feet (negative value to quit):
Last edited on
Try removing the else on line 35.
Check this, i have removed logic error and unnecessary variables:
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
33
34
35
36
37
38
#include <iostream>
#include <iomanip>                              				
using namespace std;                           					
int main()                                      				
{   
	cout << "Motion Analysis!";
	double s1 = 0,             //Initial starting position
		   s2,                 //Final position                 
		   timeInt,            //Uniform time interval
		   v1=0,v2,            //v1 and v2 are both average velocities
                               //of two consecutive intervals 
		   avgAccel;
      
     cout << fixed <<setprecision(2);
     cout << "\nEnter the uniform time interval in seconds: ";
	 cin >> timeInt;
	    	     	             
	while (1)
	{ 
		cout << "\nEnter a position in feet (negative value to quit): ";
		cin >> s2;       //s1 is initialized to 0 for first use 
            
            if  (s2 <= 0)
				break;
									
         else	
         {
                v2 = (s2-s1) / timeInt;
				avgAccel = (v2-v1) / timeInt;
				s1=s2;   //Now the final position for the current interval
				         //is the initial position of comming interval
				v1=v2;   //Similar to as mentioned above
          }
 	  cout << "\nAverage velocity (this interval): "<<v2<<" feet/second"; 
	  cout <<"\nAverage acceleration: " <<avgAccel<<" feet/second";
	}
	return 0;
}

Amazing, Thank you!!

The only thing it doesn't do is give an error message when you enter a number lower than the previous position. I will see if I can figure that out.

I am only 3 weeks into my summer course right now and I am trying to decide if this will be a good career for me. any suggestions?
Things not to do on the Internet:
5. Ask for advice on life-changing decisions and then follow it.

This is why there are career counselors.
I was only being friendly.
Well, I was being helpful.
Topic archived. No new replies allowed.