Simple Repitition Structure Output Help

I've gotten the code to do exactly what I want, but, as usual, I'm having issues with having the output come out the way i want it.

Basically, it runs the equation until x < 0. Then it stops printing.
However, I need "t" to start at 0. But when I set t = 0, the first line that prints is t = .1 . When I start t at negative .1, it gives me a weird exponential number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std

int main() {
     float d, x, g = 9.81, t = 0;

     cout << ">";
     cin >> d;

     do {
          x = d-.5*g*pow(t, 2);
          t += .1;

               cout << "< t:" << t;
               cout << "; ";
               cout << "x: " << x << endl;
          } while (x > 0);

          return 0;
}
Last edited on
1) Notice that you incrementing your t first time before printing anything.
2) 0.1f + 0.1 != 0. Why are you using floats? Native floating point type is double for more than dozen years.
It's for a class with VERY explicit instructions to familiarize ourselves with past and present methods. :D

But thank you! Huge help and helped me learn something!
Topic archived. No new replies allowed.