View previous topic :: View next topic |
Author |
Message |
homfray
Joined: 19 Nov 2003 Posts: 45 Location: Oxford
|
getc |
Posted: Thu Jun 22, 2006 5:14 am |
|
|
Ok I am having on of those days and this could be the fastest ever answer
Code: |
while(1)
{
answer=0;
if(kbhit()) //if keyboard is hit
{
c = getc() & 0x5F;
if(c == 'D')
{
printf("\n\rPress 1-6\n\r");
answer=getc();
answer=answer+200;
printf("%u",answer);
}
}
}
|
In this short code when I press D and then 1 why does't the answer come out as 201.
What I would like to do is some calculations based on the number typed in
any help would be grand
nice _________________ Nice!!! |
|
|
rberek
Joined: 10 Jan 2005 Posts: 207 Location: Ottawa, Canada
|
|
Posted: Thu Jun 22, 2006 6:15 am |
|
|
Answer will be the ASCII value of 1 , which is 49 decimal. Subtract 48 from answer to get the number you want. |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Thu Jun 22, 2006 6:23 am |
|
|
The problem is that you are adding "apples" + "oranges", then you make a decimal convertion and surelly your result is �249�
answer=getc();
Here, after you pressed �1�, answer = 0x31. // �1�= 0x31 (see ASCII table)
answer=(answer & 0x0F)+200; // ignore the high nibble of �answer� to get the �1�.
printf("%u",answer); // now you will get the expected �201�
Humberto |
|
|
|