Not accepting input

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

using namespace std;

int main()
{
  short int x;
  cin >> x;
  cout<<"X variable: " << x << "\n";
  cout<<"Memory Address of x: " << &x << "\n";
  cin.get();
}


Program gets to cin >> x; then quits after input is given. Why is this happening? I know it has to do with the way I'm inputting integer x but I can't quite put my finger on it.
I know it has to do with the way I'm inputting integer x


How are you inputting it?
cin >> x;

Right as the program begins, the user types in any integer to be assigned to x. Problem is, the program quits right after the number is given.
I suggest to read this thread:
http://cplusplus.com/forum/beginner/1988/

edit

btw, why your function doesn't return anything?
Last edited on
It doesn't have to return anything. I don't see why people always use return 0, it isn't really required.
...Yes, but it is standard C and C++. Functions that have a non-void return type should have a return statement -- programmers expect to see one. It doesn't take that much just to type it in, and your code won't break on anyone's machine.

Between lines 8 and 9 you need to clear all input to the end of the line. Add
8.5 cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

and don't forget to #include <limits> at the top.

Good luck!
Topic archived. No new replies allowed.