Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

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

, 31 May 2010
Rate this:
Please Sign up or sign in to vote.
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;
}
 
 
..

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

About me:
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.

Comments and Discussions

 
GeneralReason for my vote of 2 It's a bit longwinded isn't it? The ... PinmemberAescleal31-May-10 10:48 
Reason for my vote of 2
It's a bit longwinded isn't it? The actual copy or transformation of the input could be done in one line.
 
Possibly not a bad solution for C (I don't program much in C anymore so I'm not highly qualified to say) but even then wouldn't you use atoi rather than hardcoding ASCI values everywhere?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 31 May 2010
Article Copyright 2010 by Software_Developer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid