Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
hi,
im having problems coding this:
the user must input a number using getch()
and according to the number a letter must be outputted
eg
2 A
22 B
222 C
3 D
the user must enter '1' after their input to signal end of letter.
much appreciated

I played around for a few mins and came up with the following. It's simple (no error checking, etc), but it seems to be working and should give you at least an idea or two if nothing else. It assumes that 's' is the fourth letter on the 7 key and 'z' is the fourth letter on the 9. Replace the square brackets with angle ones where necessary.
#include [iostream.h]
#include [conio.h]int main()
{
char strCode[6];
char numPad[4][9]={{"ADGJMPTW"},{"BEHKNQUX"},{"CFILORVY"},{"XXXXXSXZ"}};
char temp;
int x=0,y=0;cout [[ "Hit 'x' to exit...\n\n\n" [[ flush;
while(1)
{while ((temp=getch())!='1' && temp != 'x')
strCode[x++]=temp;if (temp=='x')
break;strCode[x--]='\0';
y=strCode[0]-50; //shift from ASCII value
cout [[ numPad[x][y] [[ flush;
x=0;}
return 0;
}

Hi,
Thanks very much for taking the time out to give me a hand. Really appreciated. But could you give me a brief run through of how it works fully....regards..declan

I'm reposting the code with some comments thrown in. Hope it helps, but if it's still unclear you may want to look up double subscripted arrays and maybe have a look at an ASCII chart to get some idea of what I did what I did.
#include [iostream.h]
#include [conio.h]int main()
{
char strCode[6]; // used to hold user input
char numPad[4][9]={{"ADGJMPTW"},{"BEHKNQUX"},{"CFILORVY"},{"XXXXXSXZ"}};
// when you line these strings up underneath each other,
// you'll notice that each column contains letters corresponding
// to a number key on the phone. ie the first column contains ABC,
// the second contains DEF, etc .
// So we have created a 'grid' of letters so that
// numPad[0][0] = 'A' numPad[0][1] = 'D'
// numPad[1][0] = 'B' numPad[1][1] = 'E'
// numPad[2][0] = 'C' numPad[2][1] = 'F'
// etc...
char temp; // receives the keypress
int x=0; //used to count number of keypresses
int y; //used to hold value of key pressedcout [[ "Hit 'x' to exit...\n\n\n" [[ flush;
while(1)
{
// get characters until the user enters a 1 or x
// and add them into the strCode char array
while ((temp=getch())!='1' && temp != 'x')
strCode[x++]=temp;if (temp=='x') //user decides to quit
break;// x now contains the number of digits entered
// (not including the '1'), but since array
// starts at element 0 instead of 1, we shift
// back a step
x--;// Look at the first character in strCode.
// Assigning it to variable 'y' gives the
// ASCII value, so we must subtract...
// 48 to get the integer value +
// 1 because lettering starts on key 2, not 1
// 1 because the array starts at 0, not 1
y=strCode[0]-50;// we now know that the user has pressed
// the 'y' key 'x' number of times
// and can use this information to
// find the appropriate letter in the
// numPad array created earlier.
cout [[ numPad[x][y] [[ flush;// reset counter to zero before next loop
x=0;}
return 0;
}

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |