12,624,902 members (36,566 online)
Tip/Trick
alternative version

3.3K views
2 bookmarked
Posted

# Rread a number input from the keyboard and then put each of its components in an array

, 31 May 2010 CPOL
 Rate this:
Description of how to read a number input from the keyboard and then put each of its components in an array
It reads a number from CMD [For example: 1234].
Then puts each of its components in an array - digitArray[3]=1, digitArray[2]=2, digitArray[1]=3, digitArray[0]=4 .

```   //         one                ten              hundred              thousand
//          1                  2                  3                    4
//digitn=digitArray[3]*1 + digitArray[2]*10 +digitArray[1]*100 +digitArray[0]*1000

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

int digit=0,digitInput=0;
int digitArray[4]={0},digitn;

/*********************************
*                               *
********************************/

void getIntKey(void)
{
digitArray[0]=0;
digitArray[1]=0;
digit=0;
digitInput=0;

while((digit<4))
{
if (kbhit())
{
digitInput=getch();

if ((digitInput>47) && (digitInput<59))
{
digitArray[digit]=(unsigned char)digitInput-48;
digit++;

}
if (digitInput==13)  { digitn=digitArray[0]; break; }
}
}
if (digitInput!=13)  digitn=digitArray[3]*1+digitArray[2]*10+digitArray[1]*100+digitArray[0]*1000 ;
printf("\n%i\n\n",digitn);
}

/*********************************
*                               *
********************************/

int main()
{
system("color 1F");     //Blue background
printf("This program by TopCoder requires you to input 4 digits  \n ");
printf("Input Digits  (ex. 1=0001 , 1234=1234)  \n ");
printf("\nInput Digits    >");
getIntKey();

printf("\ndigitArray[3]=%d \n",digitArray[3]);
printf("digitArray[2]=%d \n",digitArray[2]);
printf("digitArray[1]=%d \n",digitArray[1]);
printf("digitArray[0]=%d \n",digitArray[0]);
printf("\n%i\n\n",digitn);

return 0;
}
```

..

## Share

 Sweden
I attended programming college and I have a degree in three most famous and successful programming languages. C/C++, Visual Basic and Java. So i know i can code. And there is a diploma hanging on my wall to prove it.
.
I am a professional, I am paid tons of cash to teach or do software development. I am roughly 30 years old .

I hold lectures in programming. I have also coached students in C++, Java and Visual basic.

In my spare time i do enjoy developing computer games, and i am developing a rather simple flight simulator game
in the c++ programming language using the openGL graphics libray.

I've written hundreds of thousands of code syntax lines for small simple applications and games.

## You may also be interested in...

 Pro Pro

 First Prev Next
 Reason for my vote of 2 It's a bit longwinded isn't it? The ... Aescleal31-May-10 11:48 Aescleal 31-May-10 11:48
 Last Visit: 31-Dec-99 19:00     Last Update: 4-Dec-16 21:01 Refresh 1