Velocity Program not working

Mar 2, 2018 at 5:52pm
I can't seem to get this program to work. Every time I compile it, it gives me
"The velocity is 78321 cm/s"

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
  #include <iostream>
#include <iomanip>
using namespace std;

int main() {

    //This program will solve an equation.

    int distance;
    int time;

    int velocity = distance / time;

    cout << "This program will display the velocity." << endl;

    cout << "Enter the distance: " << endl;
    cin >> distance;
    cout << "Enter the time:" << endl;
    cin >> time;

    system("Pause");

    cout << "The velocity is " << velocity << " cm/s." << endl;






    return 0;
}
Mar 2, 2018 at 5:59pm
You first try to calculate the velocity but input values later. Before you take user's input the values contain garbage and of course the calculation doesn't make sense.
Move line 12 to line 20. And I think you need variables to be floats instead of ints for better precision.
Mar 26, 2018 at 2:43pm
Thank you so much. :)
Topic archived. No new replies allowed.