Program won't wait for user input

Hello,

I've just started learning c++ and for my first attempt at it I thought I'd make a program which calculates final velocity when given initial velocity, acceleration and time.

The problem I'm having is that the program skips over the last input variable (t) and treats it as if it were zero. I've tried searching the web for an answer, but they all seem to be quite complicated and I still haven't been able to make it work.

Can anyone spot my error?

Here's the code:

#include <iostream>
using namespace std;

int main()
{

// declaring variables
int v_1 , v_2, a , t;

/* where:
v_1 is the initial velocity
v_2 is the final velocity
a is the acceleration
t is the time elapsed
*/

// input variables
cout << "Enter the initial velocity (in m/s)\n";
cin >> v_1;

cout << "Enter the acceleration (in m/s^2)\n";
cin >> a;

cout << "Enter the time elapsed (in s)\n";
cin >> t;
cout << "\n";

// calculate v_2
v_2 = v_1 + (a*t);

// display result
cout << "The final velocity is:\n";
cout << v_2;
cout << " m/s";

// terminate program
return 0;
}


Thank you in advance!
Last edited on
In your code, it doesnt appear to have any header. Put - #include <iostream>
at the top.

EDIT: Also, you would have to put using namespace std; at the top. (after #include <iostream> ) This is because everytimt you use cout you have to put std:: before it.
Last edited on
Ah, yes sorry. That is there, I just didn't copy and paste that part. Will edit and fix.
works fine for me...3 inputs and a final calculation with output
All the variables are of type int. What input data are you using? For example, are there decimal points in the input values? If so, that could cause an input error, which would affect any subsequent input.

Even if that isn't the cause, normally in this type of problem, it would be usual to use floating point variables throughout, (type double instead of int).

Use int for values that will always be an integer, such as a loop counter, length of a string etc.
Last edited on
Thank you Chervil! That was indeed the problem. I was trying it with a decimal point in my acceleration value.
Topic archived. No new replies allowed.