Filling in an integer array with user input

Hey there..
I am writing a program, and I need my user to input a 5 digit number. I then need to store that into a "short" variable. I know how to get userinput when using character arrays with cin.get and cin.getline, but C++ can't let me do it with integers. Can someone point me to how can I accomplish this?
Thanks a lot in advance.

Maybe
1
2
short num;
cin >> num;

Or did I overlook something?
Last edited on
Well I need to store the digits of a 5 digit number into the slots of an array

1
2
3
4
5
6

short num[5];

cout << "Enter num: ";
cin >> num;



This won't work as I have a short array.

cin.get won't work also since c++ says it can't convert pointers to ints..
Last edited on
You'll need to iterate through the array, getting the values for each element. You may want to study this link:
http://www.cplusplus.com/doc/tutorial/arrays/
This should work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
	short foo[5];
	int bar;
	cin >> bar;
	for (int nIndex = 4; bar != 0; nIndex--)
	{
		foo[nIndex] = bar % 10;
		bar = floor(bar / 10);
	}
}
Topic archived. No new replies allowed.