Not accepting input

Aug 19, 2009 at 7:07pm
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.
Aug 19, 2009 at 7:21pm
I know it has to do with the way I'm inputting integer x


How are you inputting it?
Aug 19, 2009 at 7:23pm
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.
Aug 19, 2009 at 7:43pm
I suggest to read this thread:
http://cplusplus.com/forum/beginner/1988/

edit

btw, why your function doesn't return anything?
Last edited on Aug 19, 2009 at 7:47pm
Aug 19, 2009 at 7:56pm
It doesn't have to return anything. I don't see why people always use return 0, it isn't really required.
Aug 19, 2009 at 9:04pm
...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.