I'm writing a program where the user inputs the time interval and current position of a moving object and the program calculates the velocity of the object. It's in a loop so this segment will repeat continually asking the user for the next position of the object. The problem is I also need to keep track of the total velocity of the object. Usually I would store it in a variable, but I can't do that because you get the velocity from the program, not from user input.
Is there any way for me to store the velocity in a variable once the program calculates it?
cout<<"\nAverage Velocity (this interval):"<<(float)current_position/(float)time_interval<<" feet/second";
The result of THAT equation is what I want to store in a variable. How do I do it? When I do it with >>cin the program expects me to input a value for the total velocity which is NOT what I want.
This is my first post here so I'm sorry in advance if I broke any forum conventions. Thanks for the help!
CodePudding user response:
You need to identify your variables before using them and while you are identifying them you can give them a value Ex:
float velocity = 1;
instead of 1 you put the value of choice and you can use the "velocity" variable as you like. I hope I helped you out. Edit: Also there is no need to \n after the cout because when you type cout in a new line after the endl; it will automatically start a new line
CodePudding user response:
Use cin >> VARIABLE_NAME;
instead of VARIABLE_NAME >> cin;
;
this will be after a cout statement.