I need some help with my c++ code

  • C/C++
  • Thread starter physics=world
  • Start date
  • Tags
    C++ Code
In summary, the program takes in a total time and step size from the user, calculates the number of steps needed, and then uses a for loop to calculate and output the acceleration, distance, and velocity at each step. However, there is an issue with the input for the total time, which is supposed to start from zero and work its way down to the user-input value, but instead, the user-input value is being used in the calculations. To fix this, a new variable should be used for the total time and the for loop should be adjusted accordingly.
  • #1
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
cout << "Enter the total time: ";
double t;
cin >> t;

cout << "Enter the step-size: ";
double step;
cin >> step;

double steps = ceil(t/step);

const double a = 9.806; // accel. due to gravity // m/(sec^2)

for (int count = 0; count <= steps; count++) {


double v = (a*t); // calculation for velocity

const double half = 0.5;
double x = (half)*(a)*(pow(t, 2)); // calculation for distance

cout << "t: " << setprecision(4) << fixed << t << "\tx: " << x << "\tv: " << v << endl;

t -= step;
}

return 0;This is the result I'm getting:

Enter the total time: 0.1
Enter the step-size: 0.01
t: 0.1000 x: 0.0490 v: 0.9806
t: 0.0900 x: 0.0397 v: 0.8825
t: 0.0800 x: 0.0314 v: 0.7845
t: 0.0700 x: 0.0240 v: 0.6864
t: 0.0600 x: 0.0177 v: 0.5884
t: 0.0500 x: 0.0123 v: 0.4903
t: 0.0400 x: 0.0078 v: 0.3922
t: 0.0300 x: 0.0044 v: 0.2942
t: 0.0200 x: 0.0020 v: 0.1961
t: 0.0100 x: 0.0005 v: 0.0981
t: 0.0000 x: 0.0000 v: 0.0000

___________________________________

What I am trying to do is to begin from zero and work my way down to 0.1000.
I still need to enter 0.1 for the total time (which is variable "t" in the code).

How would I code it in order to make it begin from zero?
 
Technology news on Phys.org
  • #2
Hey physics=world.

You are setting your t as what you input. Instead what you should do is use another variable (say tstep), set it equal to zero and then instead of using t -= step you use tstep += step.

Take a look at your code again to see what's happening.
 
  • #3
chiro said:
Hey physics=world.

You are setting your t as what you input. Instead what you should do is use another variable (say tstep), set it equal to zero and then instead of using t -= step you use tstep += step.

Take a look at your code again to see what's happening.

Are you saying get rid of my input "t"?
 
  • #4
physics=world said:
Are you saying get rid of my input "t"?

I see what your talking about. The "t" in both equation is not suppose to be the value taken from the user.
Thanks for helping me! :)
 
  • #5


It seems like you are trying to create a loop that starts from 0 and counts up to a certain value. To do this, you can replace the "for" loop in your code with a "while" loop. Here is an example of how you could modify your code to start from 0 and count up to 0.1:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
double t = 0; // initialize t to 0
cout << "Enter the total time: ";
double end_time;
cin >> end_time;

cout << "Enter the step-size: ";
double step;
cin >> step;

const double a = 9.806; // accel. due to gravity // m/(sec^2)

while (t <= end_time) { // loop until t reaches the end_time
double v = (a*t); // calculation for velocity
const double half = 0.5;
double x = (half)*(a)*(pow(t, 2)); // calculation for distance
cout << "t: " << setprecision(4) << fixed << t << "\tx: " << x << "\tv: " << v << endl;
t += step; // increment t by the step size
}
return 0;
}

I hope this helps! Please let me know if you have any further questions or need clarification.
 

What is C++ code?

C++ is a computer programming language commonly used for creating high-performance applications, operating systems, games, and more. It is an extension of the C programming language and is known for its speed, efficiency, and flexibility.

Why do I need help with my C++ code?

C++ can be a complex and challenging language to learn, and even experienced programmers may encounter difficulties with their code. Seeking help from others can provide fresh perspectives and solutions to problems that may seem insurmountable.

How can I improve my C++ code?

There are many ways to improve your C++ code, such as practicing good coding habits, using appropriate data structures and algorithms, and optimizing your code for better performance. Seeking feedback from others and constantly learning and updating your skills can also help improve your code.

Where can I find resources for learning and improving my C++ skills?

There are many online resources available for learning and improving your C++ skills, including tutorials, forums, and online courses. You can also join programming communities and attend workshops or conferences to connect with other programmers and learn from their experiences.

What should I do if I encounter errors in my C++ code?

When encountering errors in your C++ code, it is important to carefully analyze the error message and review your code for any mistakes. You can also consult online resources or seek help from more experienced programmers. Debugging tools can also be useful in identifying and fixing errors in your code.

Suggested for: I need some help with my c++ code

Back
Top